- In the Code-First approach, you can focus on the Domain Driven Design and start creating classes as per your domain requirement rather than design your database first and then create the classes which match your database design.
- Code-First APIs will create the database on the fly based on your entity classes and configuration.
- You can pass the following parameters in the base constructor:
- No Parameter: If you do not pass the parameter then it creates the database in your LocalDb with a name that matches your {Namespace}.{Context class name}.
- Name: If you pass "Name" parameter then it creates database in the LocalDb using that name.
- Connection String Name: If you pass connection string name of app.config or web.config then it will create the database as per a connection string.
Database Initialization Strategies in Code-First
- CreateDatabaseIfNotExists: This is default initializer.
- If you change the model class and then run the application with this initializer, then it will throw an exception.
- DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes have been changed.
- You don’t have to worry about maintaining your database schema, when your model classes change.
- DropCreateDatabaseAlways: This initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not.
- This will be useful, when you want fresh database, every time you run the application, while you are developing the application.
- Custom DB Initializer: You can also create your own custom initializer.
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());
}
}
- You can also create your custom DB initializer, by inheriting one of the intializers as shown below:
public class SchoolDBInitializer : DropCreateDatabaseAlways<SchoolDBContext>
{
protected override void Seed(SchoolDBContext context)
{
base.Seed(context);
}
}
- Suppose for the production environment you don’t want to lose existing data, then you can turn off the initializer:
Database.SetInitializer<SchoolDBContext>(null);
Seed Database
- You can insert data into your database tables during the database initialization process.
- To seed data into your database, you have to create custom DB initializer and override the Seed method.
public class SchoolDBInitializer : DropCreateDatabaseAlways<SchoolDBContext>
{
protected override void Seed(SchoolDBContext context)
{
var defaultStandards = new List<Standard>
{
new Standard {StandardName = "Standard 1", Description = "First Standard"},
new Standard {StandardName = "Standard 2", Description = "Second Standard"},
new Standard {StandardName = "Standard 3", Description = "Third Standard"}
};
foreach (var std in defaultStandards)
context.Standards.Add(std);
base.Seed(context);
}
}
Inheritance Strategy