Skip to content

Instantly share code, notes, and snippets.

@mvalipour
Created July 30, 2016 08:58
Show Gist options
  • Select an option

  • Save mvalipour/b4424a32a2c93bfe1a4d404c90016cac to your computer and use it in GitHub Desktop.

Select an option

Save mvalipour/b4424a32a2c93bfe1a4d404c90016cac to your computer and use it in GitHub Desktop.
EF Database-First but make sure code-first migrations are not ahead
public class RequireDatabaseToBeUpToDate<TContext, TMigrationsConfiguration> : IDatabaseInitializer<TContext>
where TContext : DbContext
where TMigrationsConfiguration : DbMigrationsConfiguration, new()
{
public void InitializeDatabase(TContext context)
{
var migrator = new DbMigrator(new TMigrationsConfiguration());
var migrations = migrator.GetPendingMigrations().ToList();
if (migrations.Any())
{
var message = "There are pending migrations that must be applied (via a script or using migrate.exe) before the application is started.\r\n" +
$"Pending migrations:\r\n{string.Join("\r\n", migrations)}";
throw new MigrationsPendingException(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment