You already created a database after running your Code-First application the first time, but what about the second time onwards? Will it create a new database every time you run the application? What about the production environment? How do you alter the database when you change your domain model? To handle these scenarios, you have to use one of the database initialization strategies.
There are four different database initialization strategies:
To use one of the above DB initialization strategies, you have to set the DB Initializer using the Database
class in a context class, as shown below:
public class SchoolDBContext: DbContext { public SchoolDBContext(): base("SchoolDBConnectionString") { Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseAlways<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new SchoolDBInitializer()); } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } }
You can also create your custom DB initializer, by inheriting one of the initializers, as shown below:
public class SchoolDBInitializer : CreateDatabaseIfNotExists<SchoolDBContext> { protected override void Seed(SchoolDBContext context) { base.Seed(context); } }
In the above example, the SchoolDBInitializer
is a custom initializer class that is derived from CreateDatabaseIfNotExists
.
This separates the database initialization code from a context class.
You can also set the db initializer in the configuration file. For example, to set the default initializer in app.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer" value="System.Data.Entity.DropCreateDatabaseAlways`1[[SchoolDataLayer.SchoolDBContext, SchoolDataLayer]], EntityFramework" /> </appSettings> </configuration>
You can set the custom DB initializer, as follows:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer" value="SchoolDataLayer.SchoolDBInitializer, SchoolDataLayer" /> </appSettings> </configuration>
You can turn off the database initializer for your application. Suppose that you don't want to lose existing data in the production environment, then you can turn off the initializer, as shown below:
public class SchoolDBContext: DbContext { public SchoolDBContext() : base("SchoolDBConnectionString") { //Disable initializer Database.SetInitializer<SchoolDBContext>(null); } public DbSet<Student> Students { get; set; } public DbSet<Standard> Standards { get; set; } }
You can also turn off the initializer in the configuration file, for example:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer" value="Disabled" /> </appSettings> </configuration>