EF Code First - Multiple Application Versions Sharing A Database

626 Views Asked by At

We have an application developed using code first in EF6. This is running quite happily on our live infrastructure.

I have recently made some changes which required some schema alterations and I've generated the appropriate migrations.

Since the application was originally deployed our release process for the live environment has changed and all applications are deployed to a Prelive area so we can validate interaction with the other applications/services and run tests before the application files are released to their live location.

The problem is that the migrations will alter the database schema when the application is run in Prelive and the live version of the application will choke on its model compatibility check.

How have other people approached this issue?

Is there any way of safely running multiple versions of a code first application against the same database?

If I can disable the model checking, will the migrations still run when the application first starts or will I have to go back to generating sql change scripts and running them manually?

1

There are 1 best solutions below

0
On

I faced the same problem. Like Nick I disabled migration in the DbContext and created a different project for Database Migration. I then added the below check in the MigratorCode so that it only upgrades 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();
}