当我在VS2015中使用ASP运行PM> Remove-Migration -context BloggingContext时。NET Core项目使用EF Core我得到以下错误:

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.

如何取消申请?我使用的是ASP的最新版本。NET Core 1.0, EF Core和VS2015更新3。


当前回答

要恢复上次应用的迁移,您应该(包管理器控制台命令):

从数据库恢复迁移:PM> Update-Database <prior-migration-name> 从项目中删除迁移文件(否则将在下一步中再次应用) 更新模型快照:PM> Remove-Migration

乌利希期刊指南: 在Visual Studio(2017)的最新版本中,第二步似乎不需要。

其他回答

在包管理器控制台:

- database就Your_Migration_You_Want_To_Revert_To

关于如何恢复迁移的更多选项和解释可以在这里看到

首先执行以下命令:

PM>update-database -migration:0

然后运行这个:

PM>remove_migration

完成

要完全删除所有迁移并重新开始,请执行以下操作:

dotnet ef database update 0
dotnet ef migrations remove

因为我在搜索ef而不是ef core中的回滚迁移时被重定向到这个问题,所以我将这个答案添加到这个问题中,供所有想知道ef中相同问题的人使用。如果你正在使用ef,你可以使用以下命令回滚迁移:

Update-Database [[-Migration] <String>] 
                [-Context <String>]
                [-Project <String>]
                [-StartupProject <String>] 
                [<CommonParameters>] 

基于问题:

Update-Database -Migration <previous-migration-name> -context BloggingContext

您应该从“_efmigrationhistory”表中删除迁移'20160703192724_MyFirstMigration'记录。

否则,下面的命令将删除迁移和删除迁移文件夹:

PMC命令:

   > remove-migration -force

CLI命令:

   > dotnet ef migrations remove -f

CLI命令链接

关于PMC命令