entity-frameworkentity-framework-6

Check for pending migrations in Entity Framework?


In Entity Framework 6 I'm using the Update-Database command to apply migrations. I've got three environments that I juggle (DEV, QA and PROD), and upgrade them using

Update-Database -ConnectionStringName DEV 

But, now I'd like to know which migration my PROD environment is at, and which migrations willl be applied if I call Update-Database.

Is there a command for checking which migration is the latest one applied, and which will be applied if I run Update-Database?


Solution

  • To see which migrations have been applied to the database use the command Get-Migrations:

    Get-Migrations -ConnectionStringName PROD
    

    You can also check the contents of the table __MigrationsHistory in the right database. It contains info about all migrations applied to the database.

    The next migration applied depends on the existing migration files in your project. A migration file name includes a prefix that is a timestamp, which specifies the time at which the migration file was generated (unless you used the -force parameter that might cause to reuse an existing migration file keeping its existing timestamp string). The migrations are applied according to that timestamp. So the alphabetical ordering of your migration files indicates the order in which they are applied.

    A safe way to check which migration will be applied next is to run Update-Database with the -Script parameter, which generates the SQL script for the migration but does not run it. So you can see which migration would be applied if you run the real Update-Database.

    Update: in Entity Framework Core this command is not available. You can still check __MigrationsHistory table contents. To generate the SQL script that would be executed without running it you can use the command Script-Migration.