Efcore: Remove-Migration runs the application

Created on 30 Aug 2017  路  1Comment  路  Source: dotnet/efcore

I'm not sure this is the right place. I was looking for the EF Core CLI, but was redirected here.

Anyway, I wanted to remove the latest migration in my ASP.Net Core / EF Core test project (AddShamesCreatedColumns), so I went back to the previous migration (Shame_AjoutColonnes) :

PM> Update-Database Shame_AjoutColonnes
Reverting migration '20170830090933_AddShamesCreatedColumns'.
Done.
PM> Remove-Migration
The migration '20170830090933_AddShamesCreatedColumns' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.

After the Update-Database command, I can see that my DB has no longer the migration applied.
But, as soon as I run "Remove-Migration", it reapplies the migration, and fails to remove it.

What I realized is that in my Startup project, I am executing a Database.MigrateAsync() :

public async Task InitializeDatabaseAsync(IServiceProvider serviceProvider)
        {
            using (IServiceScope serviceScope = serviceProvider.CreateScope())
            {
                WotContext db = serviceScope.ServiceProvider.GetService<WotContext>();
                await db.Database.MigrateAsync();
            }
        }

As soon as I removed the await db.Database.MigrateAsync(); line, the Remove-Migration worked.

It seems like it is executing my project, running the await db.Database.MigrateAsync(); query, thus updating my database.

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.3

And if this helps:

PM> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  502     

It seems like I'm not the only one having the issue: https://stackoverflow.com/questions/45941707/why-remove-migration-run-my-app

Thanks

closed-question

Most helpful comment

After re-reading the answer on stackoverflow, it seems like I should not Migrate the database inside the Startup but inside the Program.Main() method.

>All comments

After re-reading the answer on stackoverflow, it seems like I should not Migrate the database inside the Startup but inside the Program.Main() method.

Was this page helpful?
0 / 5 - 0 ratings