IT:AD:EF/CodeFirst/Migrations:HowTo:Define a MigrateDatabaseToLatestVersion Db Initializer
Summary
Until CodeMigrations became generally available, we had to use Initializers that dropped and recreated and reseeded databases.
Important: This is no longer best practice.
Process
What we do now is at some point prior to adding seeding switch to using a migration capable initialiser.
And then we add creation of Seeding stuff beside the new database scripts.
Note that this potentially affects
- XActLib seeding concepts…damn.
Example
public class AppDbMigrateDatabaseToLatestVersion_Initializer : MigrateDatabaseToLatestVersion<AppDbContext,Migrations.Configuration>
{
private readonly string _typeName;
private readonly ITracingService _tracingService;
public AppDbMigrateDatabaseToLatestVersion_Initializer(ITracingService tracingService)
{
_typeName = this.GetType().Name;
//Repository settings were used for when not being built using CodeFirst.
//Keeping it just in case
_tracingService = tracingService;
}
public new void InitializeDatabase(AppDbContext context)
{
base.InitializeDatabase(context);
//This should only be applied if Db was dropped...
//this.Seed(context);
}
protected void Seed(AppDbContext dbContext)
{
}
}