Efcore: Look for pending migrations and show user progress of migration while migrating

Created on 10 Jul 2016  路  2Comments  路  Source: dotnet/efcore

Hi,

I would like to inform the user if there are open migrations before they are applied.
Is there currently a way to do this in wpf and windows Forms projects?

Sometimes the migrations can take very long and therefor I would like to show the user a progressbar with the status of the migration, otherwise he could kill the process and destroy the database, because he doesn't know that the migration is still running.
Is there already an option available to do this?

regards
Benny

closed-question

All 2 comments

Here is the code to find the pending migrations and then apply them one at a time so that you can write status etc.

``` c#
using (var context = new TestContext())
{
var all = context.GetService().Migrations.Select(m => m.Key);
var applied = context.GetService().GetAppliedMigrations().Select(m => m.MigrationId);
var pending = all.Except(applied).ToArray();

var migrator = context.GetService<IMigrator>();
for (int i = 0; i < pending.Length; i++)
{
    Console.WriteLine($"Applying {pending[i]} {i + 1}/{pending.Length}");
    migrator.Migrate(targetMigration: pending[i]);
}

}
```

Also opened #6110 to add the ability to get all/applied/pending migrations with a top level API (as having to write all that code in the above example is pretty ugly for a relatively simple task).

Was this page helpful?
0 / 5 - 0 ratings