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

Why does the entity framework need an ICollection for lazy loading?

I think i found the solution…See here for more details: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/47296641-0426-49c2-b048-bf890c6d6af2/ Essentially you want to make the ICollection type protected and use this as the backing collection for the public IEnumerable public class Product { // This is a mapped property protected virtual ICollection<Photo> _photos { get; set; } // This is an un-mapped property … 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