Code First DBInitiazation Plans

Database Initialization Strategies in EF6 Code-First
The following questions arises in developers mind after creating database using Code-First approach.
Question 1:you have created a database after running your code-first application for first time,but what about second time.
Question 2:Will it create a database every time you run the application?
Question 3:How do you alter database when you change your domain model.
The following are Database Initialization Strategies:
1.CreateDatabaseIfNotExists:This is default initializer.It says,this will create the database if none exists as per the configuration.
But if you change the model class and then run the application with this initializer then it will throw error.
2.DropCreateDatabaseIfModelChanges:It drops the existing database and creates a new database.If your model classes have been changed.
3.DropCreateDatabaseAlways:It drops an existing database every time you run the application irrespective of whether your model classes have changed or not.
4.Custom DB Initializer:Here you can create your own custom initializer.
In order to use one of the above DB initialization strategies,you have to set the DB initializer using the database class in context class.
for eg:-


public class customerDBContext:DbContext
{ public customerDBContext():base('customerDBConnectionstring')
{ Database.SetInitializer<CustomerDBContext>(new CreateDatabaseIfNotExists<customerDBContext()>); } } public class CustomDBInitializer:CreateDatabaseIfNotExists<customerDBContext> { protected override void Seed(CustomerDBContext context) { base.seed(context); } }

you can also set db initializer in configuration file called app.config. You can also Turn off DB initializer: if you dont want to lose existing data,then you can turn off initializer. In customDBContext , constructor just include the following line Database.SetInitializer(null);.