6

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Mike B
  • 2,660
  • 3
  • 20
  • 22

2 Answers2

20

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(); 
}
Stephen M. Redd
  • 5,378
  • 1
  • 24
  • 32
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • I've not got around to implementing this yet, but that looks just like what I'm looking for. – Mike B Aug 07 '13 at 03:22
4

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

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 1
    In case of manually added migration steps (e.g. an enum value has changed and you might apply a script to update the corresponding field in the database) `DbContext.Database.CompatibleWithModel()` will return `true`. That doesn't match the original request! – Marcel Jan 31 '17 at 16:50