Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Check for pending migrations

In our production environment, we have an automated deploy script that takes down our site, runs migrations, and then brings it back online. We'd like to avoid taking the site down by just switching to the new code when there aren't any migrations that need to be run.

Does entity framework have a command like "Update-Database" that would allow us to check if there are migrations to run?

like image 732
Mike B Avatar asked Jul 29 '13 19:07

Mike B


2 Answers

The DbMigrator class has the GetPendingMigrations method which sounds like the exact one you look for. It should be something like

YourMigrationsConfiguration cfg = new YourMigrationsConfiguration(); 
cfg.TargetDatabase = 
   new DbConnectionInfo( 
      theConnectionString, 
      "provider" );

DbMigrator dbMigrator = new DbMigrator( cfg );
if ( dbMigrator.GetPendingMigrations().Any() )
{
   // there are pending migrations
   // do whatever you want, for example
   dbMigrator.Update(); 
}
like image 151
Wiktor Zychla Avatar answered Sep 30 '22 01:09

Wiktor Zychla


I use DbContext.Database.CompatibleWithModel() with EF 6.1.3

like image 27
Kirsten Avatar answered Sep 30 '22 01:09

Kirsten