Entity Framework Database Initialization four strategies

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.