We use entity framework 6 in our application, along with a Custom Sql Server Migration Generator that handles some edge cases when generating migrations.
We use add-migration to add a new migration to the system, and then when the application is deployed, our deployment tool runs migrate.exe to update the target database with any pending migrations.
However, we've discovered that the migrations are being applied automatically ANYWAY to any database we happen to connect to while running the application. This is highly undesirable behaviour for our application. We want it to not work if there is a mismatch between the application model and the database it is trying to connect to.
I believe this is because we are using the following:
// Supply configuration reference to Initializer
Database.SetInitializer(new MigrateDatabaseToLatestVersion<OurContext, OurDb.Migrations.Configuration>(useSuppliedContext: true));
I could remove this and replace it with a different initializer (e.g. CreateDatabaseIfNotExists) but then the Configuration would not be used, because the constructor does not have a parameter for configuration:
// No opportunity to supply configuration?
Database.SetInitializer<OurDb>(new CreateDatabaseIfNotExists<OurDb>());
So my question is, how do I retain the custom sql generator while disabling the automatic migrations?
Here's part of our Configuration.cs class:
internal sealed class Configuration : DbMigrationsConfiguration<OurDb>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
SetSqlGenerator("System.Data.SqlClient", new CustomSqlServerMigrationSqlGenerator());
}
protected override void Seed(OurDb context)
{
//
}
}