Forcing code-first to always initialize a non-existent database?

Initializer is executed when you need to access the database so if you want to create database on app start use anything of the following: context.Database.Initialize(true); //If set to true the initializer is run even if it has already been run. context.Database.Create() http://msdn.microsoft.com/en-us/library/system.data.entity.database.initialize(v=vs.103).aspx CreateDatabaseIfNotExists An implementation of IDatabaseInitializer that will recreate and optionally re-seed the … Read more

Entity framework code first delete with cascade

You mentioned EF code first which means EF 4.1 but you have shown example of deleting object in EF 4. Correct approach to delete object in EF 4.1 without loading it from database is: var category = new Category() { CategoryId = 1 }; context.Categories.Attach(category); context.Categories.Remove(category); context.SaveChanges(); If you didn’t change anything in configuration of … Read more

How can set a default value constraint with Entity Framework 6 Code First?

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

Getting exact error type in from DbValidationException

While you are in debug mode within the catch {…} block open up the “QuickWatch” window (ctrl+alt+q) and paste in there: ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors This will allow you to drill down into the ValidationErrors tree. It’s the easiest way I’ve found to get instant insight into these errors. For Visual 2012+ users who care only about the … Read more

Modelling polymorphic associations database-first vs code-first

I personally stick with Database first when using EF on any schema that is this level of complexity. I have had issues with complex schemas in regards to code first. Maybe the newer versions are a little better, but worrying how to try and code complex relationships seems less straight forward then allowing the engine … Read more

Integration Testing Entity Framework code first with in-memory database

Use SQL Compact 4.0 (download both SqlCE and tools by web platform installer) – EF Code first has direct support for that. The only difference will be that your application will use connection string to big SQL Server: <add name=”MyDbContext” provider=”System.Data.SqlClient” connectionString= “Data Source=…;InitialCatalog=…;Integrated Security=SSPI” /> and your tests will use connection string to SQL … Read more

Why does EF5 code first use datetime2 when inserting a nullable datetime into the database?

The DateTime type in .NET has the same range and precision as datetime2 in SQL Server. When EF inserts or updates a datetime or datetime2 column in SQL Server it converts the model property to the type that can hold the whole range of DateTime in .NET, that’s datetime2. Converting into datetime would fail if … Read more

How should I access a computed column in Entity Framework Code First?

I have a somewhat of an workaround. You can only use calculated field on a existing database. If you add your property to CF object as: [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public decimal TotalSources { get; set; } and if you add a line in your script that will delete information about generation of that database: DELETE FROM [dbo].[EdmMetadata] … Read more