Policy one: Re-create the database when the database does not exist
1 database.setinitializer<testcontext> (new createdatabaseifnotexists<testcontext> ( ));
Policy two: Create a database each time the application is started
1 database.setinitializer<testcontext> (new dropcreatedatabasealways<testcontext> () );
Policy three: Re-creating the database when the model changes
1 database.setinitializer<testcontext> (new dropcreatedatabaseifmodelchanges< Testcontext> ());
Strategy Four: Never create a database
1 database.setinitializer<testcontext> (null);
Entity Framework Database Initialization Example
1 usingSystem.Data.Entity;2 usingSystem.Data.Entity.Infrastructure;3 usingWeb.Models.Mapping;4 5 namespaceWeb.models6 {7 Public classTestcontext:dbcontext8 {9 StaticTestContext ()Ten { OneDatabase.setinitializer<testcontext> (NULL); A } - - PublicTestContext () the:Base("Name=testcontext") - { - } - + Publicdbset<person> people {Get;Set; } - + protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) A { atMODELBUILDER.CONFIGURATIONS.ADD (NewPersonmap ()); - } - } -}
Entity Framework Database Initialization four strategies