In this article you will understand the Database Initializer in Entity Framework Code-First Approach.
To add initializers to our dbcontext we need the following things in dbcontext class.
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.
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.
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.
Node Js and Angular CheatSheet