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:
- 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 .
- 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.
- 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.
- 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