Entity Framework Code-first (+): Database initialization strategy

Source: Internet
Author: User

Database initialization Strategies in Code-first:

You already created a database after running your Code-first application the first time and what's about the second time on Wards?? Would it create a new database every time you run the application? What's the production environment? How does you alter the your domain model? To handle these scenarios, you have to use one of the database initialization strategies.

There is four different database initialization strategies:

  1. createdatabaseifnotexists: This is the default initializer. As the name suggests, it would create the database if none exists as per the configuration. However, if the model class and then run the application with this initializer, then it'll throw an exception .
  2. dropcreatedatabaseifmodelchanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) has been C Hanged. So you don ' t has to worry the maintaining your database schema, when your model classes change.
  3. dropcreatedatabasealways: As the name suggests, this initializer drops a existing database every time you run the application, irrespective of whet Her your model classes has changed or not. This is being useful, when you want fresh database, every time you run the application, and like while you are developing the A Pplication.
  4. Custom DB Initializer: You can also create your own custom initializer if any of the above doesn ' t satisfy your requirements or you want to do s ome other process that initializes the database using the above initializer.

To use one of the above DB initialization strategies, you have to set the DB Initializer using Database class in Context C Lass, as shown below:

 Public classSchooldbcontext:dbcontext { PublicSchooldbcontext ():Base("schooldbconnectionstring") {Database.setinitializer<SchoolDBContext> (NewCreatedatabaseifnotexists<schooldbcontext>()); //database.setinitializer<schooldbcontext> (New dropcreatedatabaseifmodelchanges<schooldbcontext> (        )); //database.setinitializer<schooldbcontext> (New dropcreatedatabasealways<schooldbcontext> ()); //database.setinitializer<schooldbcontext> (New Schooldbinitializer ());    }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; }}

You can also create your custom DB initializer, by inheriting one of the initializers, as shown below:

 Public class Schooldbinitializer:  createdatabaseifnotexists<schooldbcontext>{    protected  Overridevoid  Seed (schooldbcontext context)    {        base. Seed (context);}    }

As can see in the above code, we have created a new class Schooldbinitializer, which are derived from Dropcreatedatabas Ealways initializer.

Set db initializer in the configuration file:

You can also set DB initializer in the configuration file. For example, to set the default initializer in app. config:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <appSettings>    <AddKey= "Databaseinitializerfortype schooldatalayer.schooldbcontext, Schooldatalayer"value= "System.Data.Entity.DropCreateDatabaseAlways ' 1[[schooldatalayer.schooldbcontext, Schooldatalayer]], EntityFramework " />    </appSettings></Configuration>

You can set custom DB initializer, as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <appSettings>        <AddKey= "Databaseinitializerfortype schooldatalayer.schooldbcontext, Schooldatalayer"value= "Schooldatalayer.schooldbinitializer, Schooldatalayer" />    </appSettings></Configuration>

This is the can use DB initialization strategy for your application.

Entity Framework Code-first (+): Database initialization strategy

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.