当我在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。


当前回答

简单地说,您可以通过值来确定迁移的目标

 Update-Database -Migration:0

然后继续并删除它

 Remove-Migration

说明该命令用于清空数据库。

其他回答

dotnet ef database update <the-migration-you-want-to-recover>
dotnet ef migrations remove

不要忘记remove- call,因为这将为您删除迁移文件并更新快照文件。

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

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

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

要取消应用特定的迁移:

dotnet ef database update LastGoodMigrationName
or
PM> Update-Database -Migration LastGoodMigrationName

取消所有迁移:

dotnet ef database update 0
or
PM> Update-Database -Migration 0

删除上次迁移:

dotnet ef migrations remove
or
PM> Remove-Migration

删除所有迁移:

只需删除迁移文件夹。

删除最后几个迁移(不是全部):

没有一个命令可以删除一堆迁移,我们不能只删除这几个迁移和它们的*.designer.cs文件,因为我们需要保持快照文件的一致状态。我们需要逐个删除迁移(参见上面删除最后一次迁移)。

取消应用并删除上次迁移:

dotnet ef migrations remove --force
or
PM> Remove-Migration -Force

一般来说,如果您正在使用Package Manager Console,删除特定迁移的正确方法是引用迁移的名称

Update-Database -Migration {Name of Migration} -Context {context}

另一种删除你根据文档应用的最后一个迁移的方法是使用命令:

dotnet ef migrations remove

这个命令应该从解决方案目录中的开发人员命令提示符(如何打开命令提示符)执行。

例如,如果你的应用程序名为“application”,在文件夹c:\Projects中。那么你的路径应该是:

C:\Projects\Application

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

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

基于问题:

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