EF Code First - Multiple Application Versions Sharing Database

We have an application developed using code first in EF6. It works pretty happily in our living infrastructure.

I recently made some changes that required some schema changes and I created migrations accordingly.

Since the app was originally deployed, the live release process has changed and all apps have been deployed to the Prelive realm, so we can test interactions with other apps / services and run tests before the app files are released to their live location, etc.

The problem is that migrations will change the database schema when the application is launched in Prelive, and the live version of the application will overwhelm the model compatibility check.

How have other people approached this problem?

Is there a way to safely run multiple versions of the first application of code in the same database?

If I can turn off model validation, will migrations be done the first time I run the app, or will I have to go back to creating the sql change scripts and start them manually?

+3


source to share


1 answer


I faced the same problem. Like Nick, I disabled migration in DbContext and created another project to migrate the database. Then I added the following check to the MigratorCode so that it only updates and never downgrades, instead of calling migrator.Update () directly.



migrationConfiguration.TargetDatabase = new DbConnectionInfo(MigrationContext.EnvironmentSettings.ConnectionString, "System.Data.SqlClient");
var migrator = new DbMigrator(migrationConfiguration);
var localMigrations = migrator.GetLocalMigrations();
var dbMigrations = migrator.GetDatabaseMigrations();

if (localMigrations.Except(dbMigrations).Any())
{
    migrator.Update();
}

      

+1


source







All Articles