Understanding Database Initializer In Entity Framework Code-First Approach

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 code-first approach the user who is working will only concentrating on creating classes, models and writing code, rest of work like creating database, creating tables, assigning keys, etc, will be look 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 code-first approach. These strategies are also called as 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 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 Code-First approach, When the application starts, this initializer check 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, for 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 suggest 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 whatever you saved in the table will delete. Here i am explaining how it will work.

    Here i have added one more field in 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 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 drop the table  and then create 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 create the database every time you run the application. If the database already exist 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 loose 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 Create button.

    Now the record will be added in 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 database 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 allow you to update existing database with new model classes and you can update the existing model class property without loosing your previous data.

Up Next
    Ebook Download
    View all
    Learn
    View all