How to unapply a migration in ASP.NET Core with EF

2019-03-07 21:26发布

When I run PM> Remove-Migration -context BloggingContext in VS2015 with an ASP.NET Core project using EF Core I get the following error:

System.InvalidOperationException: The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force) 
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
 The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.

How can I unapply it? I'm using latest release of ASP.NET Core 1.0, EF Core, and VS2015 Update 3.

11条回答
贼婆χ
2楼-- · 2019-03-07 22:18

Simply you can target a Migration by value

 Update-Database -Migration:0

Then go ahead and remove it

 Remove-Migration
查看更多
beautiful°
3楼-- · 2019-03-07 22:19

Use dotnet ef database update <previous-migration-name>

Then try to remove last migration.

Removing migration without database update doesn't work because you applied changes to database.

查看更多
对你真心纯属浪费
4楼-- · 2019-03-07 22:19

In Package Manager Console:

Update-Database Your_Migration_You_Want_To_Revert_To

More options and explanation on how to revert migrations can be seen here

查看更多
Juvenile、少年°
5楼-- · 2019-03-07 22:21

You can still use the Update-Database command.

Update-Database -Migration <migration name> -Context <context name>

However, judging by the name of your migration i'm assuming it's the first migration so that command may not work. You should be able to delete the entry from the __MigrationHistory table in your database and then run the Remove-Migration command again. You could also delete the migration file and just start again.

查看更多
Viruses.
6楼-- · 2019-03-07 22:25

1.find table "dbo._EFMigrationsHistory", then delete the migration record that you want to remove. 2. run "remove-migration" in PM(Package Manager Console). Works for me.

查看更多
登录 后发表回答