Understanding Database Initializer In Entity Framework Code-First Approach

The Code-First approach allows you to define model classes as per the Domain requirements. Hence, you have complete control over the classes being written or implemented.
 
In the code-first approach the user who is working will only concentrate on creating classes, models, and writing code, the rest of the work like creating a database, creating tables, assigning keys, etc, will be looked over by the Entity framework.
 
Thus here the developer will not get the overload of backend works.
 
The Code-First approach has there own principle or strategy. These strategies are the main backbone of the code-first approach. These strategies are also called database Initializers.
 
Before going deep into the code, firstly, I would like to give a brief introduction about what these approaches are.
 
Here, I have explained the Database initializers and how they work in creating and updating records in the model.
 
 
To add initializers to our dbcontext we need the following things in dbcontext class. 
 
 
For using all these we need a namespace that is using System.Data.Entity;
 
So here I am explaining each and every database initializer and how we can use them in our project. 
  1. CreateDatabaseIfNotExists: 
     
    This is the default initializer class used by the Code-First approach, When the application starts, this initializer checks for the required model database. If it is not found it will create the database. Suppose we have created a new project and added a model with different property. Now we will create a controller, that include all CRUD operation views and a dbcontext.
     
    Suppose, I have these 3 properties in my model. For this create a new project as in the following screenshot.
     
     
    Select a project and then select MVC 4 template.
     
     
    Click Ok and select the following template.
     
     
    Now add a model as in the following.
     
     
    Now add a controller.
     
     
     
    Now run your application.
     
     
    Here as per our model, we can see the two fields in the view.
     
     
    Now when we add a record it will get saved in our local database and shown as follows.
     
     
    Now when we will check server explorer a database will automatically get created and inside that, a table of employee created automatically and the record will be added there.
     
     
    Now when we will check employee table we will get the record.
     
     
    Thus as the default database initializer is CreateDatabaseIfNotExists as there is no database in starting it will create the database and table itself.
     
  2. DropCreateDatabaseWhenModelChanges:
     
    As the name suggests this database initializer will drop the table each time you change the model. The main demerit of this database initializer is that the previous data you saved in the table will delete. Here I am explaining how it will work.
     
    Here I have added one more field to the model.
     
    Now here I have compared the two models.
     
     
    Now, I will build the solution and overwrite the controller. And initialise the "DropCreateDatabaseWhenModelChanges".to the datacontext constructor.
     
     
    Now, I will override my controller for updating the view for new property and one record.
     
     
    When I click to add this data it will be shown as follows.
     
     
    But here we are unable to see the first record which we have uploaded. This is because when we run this application the "DropCreateDatabaseWhenModelChanges" initializer first drops the table and then creates a new table. When we check the local table the only record which we have inserted will show.
     
     
  3. DropCreateDatabaseAlways:
     
    This strategy of code first approach creates the database every time you run the application. If the database already exists and there is no modification done in the model, it will drop the database and create a new one. In this approach, you will also lose the data.
     
    Now here I am changing the database initializer to DropCreateDatabaseAlways as in the following screenshot,
     
     
    Now just build the application and run it.
     
     
    Add a new record and press the Create button.
     
    Now the record will be added to the view and shown as follows.
     
     
    Now here also we can see that the previous record also deleted.
     
     
    Thus in both of these approaches, the saved data will be lost.

Conclusion

 
The above three databases initializing approach fails when you add new model classes  or property to the existing model. To overcome the demerits of Entity-framework code first approach a new concept called “Entity-framework code first Migration” came into picture.
 
This allows you to update an existing database with new model classes and you can update the existing model class property without losing your previous data.


Recommended Free Ebook
Similar Articles