Entity Framework/MVC3: temporarily disable validation
You just need to set Configuration.ValidateOnSaveEnabled = false in your context class before calling SaveChanges(). context.Configuration.ValidateOnSaveEnabled = false; context.SaveChanges();
You just need to set Configuration.ValidateOnSaveEnabled = false in your context class before calling SaveChanges(). context.Configuration.ValidateOnSaveEnabled = false; context.SaveChanges();
Had to install Microsoft.EntityFrameworkCore.Relational to fix the issue. Edit: Credit goes to Ivan Stoev for finding this out
The problem is this one: MyEntity has an ID of 0 since it’s a new MyEntity. The Group is also new and contain a reference to MyEntity. So, MyEntity contains a list of Group which contain a reference back to MyEntity. The problem is that MyEntity.Group.MyEntity seems to be “new and not the same” as … Read more
To ensure that lazy loading of a navigation property will work after you’ve created the parent you must not create the Survey with the new operator but create it by means of the context instance because it will instantiate a dynamic proxy that is capable to lazily load the related Client. That’s what the DbSet<T>.Create() … Read more
Use EntityFramework.DynamicFilters. It allows you to create global filters that will be applied automatically (including against navigation properties) when queries are executed. There is an example “IsDeleted” filter on the project page that looks like this: modelBuilder.Filter(“IsDeleted”, (ISoftDelete d) => d.IsDeleted, false); That filter will automatically inject a where clause on any query against an … Read more
For that you can use the Select method: PersonQuery.Include(x => x.Addresses.Select(a => a.Mailbox)); You can find other examples in here and here.
You can use DateCreated = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”) Usage: public partial class MyMigration : DbMigration { public override void Up() { CreateTable(“dbo.Users”, c => new { Created = c.DateTime(nullable: false, defaultValueSql: “GETDATE()”), }) .PrimaryKey(t => t.ID); … Update 2012-10-10: As requested by Thiago in his comment, I add a little extra context. The code … Read more
If you want to use Time type in database you will have to use TimeSpan with 24 hour cycle in your application. DateTime is not representation of time.
you should use the EntityConnectionStringBuilder class string providerName = “System.Data.SqlClient”; string serverName = “.”; string databaseName = “AdventureWorks”; // Initialize the connection string builder for the // underlying provider. SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); // Set the properties for the data source. sqlBuilder.DataSource = serverName; sqlBuilder.InitialCatalog = databaseName; sqlBuilder.IntegratedSecurity = true; // Build the SqlConnection … Read more
Unfortunately the answer right now is ‘No’. But you can vote for Better support for default values EDIT 30 Mar 2015: It’s coming in EF7… Support database default values in Code First EDIT 30 Jan 2017: General support for default database values is part of EF Core (the new name for EF7)… Default values EDIT … Read more