EF6 How do I retain a custom sql generator while disabling automatic migrations

c# ef-code-first entity-framework-6

Question

In our application, we use entity framework 6 together with a customized SQL Server migration generator that takes into account various edge circumstances while producing migrations.

Our deployment tool executes migrate.exe to update the target database with any outstanding migrations after adding a new migration to the system using add-migration.

However, we've found that while the application is running, the migrations are being done AUTOMATICALLY TO ANY database we chance to connect to. For our application, this behavior is quite undesired. If the application model and the database it is attempting to connect to are incompatible, we want it to fail.

This, in my opinion, is caused by the fact that we make use of:

// Supply configuration reference to Initializer
Database.SetInitializer(new MigrateDatabaseToLatestVersion<OurContext, OurDb.Migrations.Configuration>(useSuppliedContext: true));

I could delete this and substitute a different initializer (like CreateDatabaseIfNotExists), but it would prevent the Configuration from being used since the constructor lacks a configuration parameter:

// No opportunity to supply configuration?
Database.SetInitializer<OurDb>(new CreateDatabaseIfNotExists<OurDb>());

Therefore, I have a concern about how to keep the custom sql generator while turning off automatic migrations.

Part of our Configuration.cs class is shown below:

internal sealed class Configuration : DbMigrationsConfiguration<OurDb>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        AutomaticMigrationDataLossAllowed = false;
        SetSqlGenerator("System.Data.SqlClient", new CustomSqlServerMigrationSqlGenerator());
    }

    protected override void Seed(OurDb context)
    {
        //
    }
}
1
4
7/29/2019 12:07:53 PM

Popular Answer

ZZZ_tmp
3
8/6/2019 9:24:33 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow