Created
July 30, 2016 08:58
-
-
Save mvalipour/b4424a32a2c93bfe1a4d404c90016cac to your computer and use it in GitHub Desktop.
EF Database-First but make sure code-first migrations are not ahead
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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