Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 279
Frode Lillerud Avatar asked Jan 07 '17 08:01

Frode Lillerud


People also ask

How does rails check for pending migrations?

Rails provides a method to check if any migration is pending. Listing relevant methods from ActiveRecord::MigrationContext class below. This piece of code raises an exception ActiveRecord::PendingMigrationError if any migration is pending. In the next section, we will see the logic used for needs_migration?

How do I run migrations in Entity Framework?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).

How do I get rid of migrations in Entity Framework?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What are migrations in Entity Framework?

The migrations feature in EF Core provides a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database.


1 Answers

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.

like image 173
Diana Avatar answered Oct 06 '22 17:10

Diana