What is the role of Database Intializer for Code First approach in Entity Framework?

 Posted by Chvrsri on 3/1/2016 | Category: ASP.NET MVC Interview questions | Views: 3452 | Points: 40

The main role of Database Intializer is to create the Database and the specified tables. When we use DBContext type to use the database for the very first time then Database Intializer will be called.

Creating New Database
Generally Database.SetInitializer() is the Initialization approach. This method will take the parameter of IDatabaseInitializer<TContext> where the TContext will be of DbContextType.

IDatabaseInitializer<TContext> has 3 implementations namely:
1) CreateDatabaseIfNotExists<TContext> (Creates a new Database)
2) DropCreateDatabaseAlways<TContext> (It Drops and Recreates database all the time)
3) DropCreateDatabaseIfModelChanges<TContext>(It Drops and Recreates only when there are some modifications to the Model)

Using Existing Database

If we want to use the existing database then we need to set the Database intializer with null so that we can modify the already existing database.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response