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

这个解决方案是为。net Core 3.1提供的,但是也应该与下一个版本兼容

删除迁移:

删除迁移将从项目中删除文件 只有当迁移没有应用到数据库时,才能删除该迁移 然而, 要删除上次创建的迁移:cd to_your_project然后dotnet ef migrations remove

注意:只有在您还没有执行dotnet ef数据库更新或在c#代码中调用database . migrate()时,删除迁移才有效,换句话说,只有在迁移还没有应用到您的数据库时才有效。

取消应用迁移(恢复迁移):

从数据库中删除不需要的更改 不从项目中删除迁移文件,但允许您在取消应用后将其删除 要恢复迁移,您可以: 创建一个新的迁移dotnet ef migrations add <your_changes>并应用它,这是微软推荐的。 或者,使用dotnet ef database update <your_migration_name_to_jump_back_to>将数据库更新为指定的迁移(基本上是取消应用或恢复未选择的迁移)

注意:如果要取消应用的迁移不包含已经在数据库中应用并正在使用的特定列或表,则该列或表将被删除,您的数据将丢失。

在恢复迁移之后,您可以删除不需要的迁移

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

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

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

dotnet ef migrations remove

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

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

C:\Projects\Application

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

dotnet ef database update 0
dotnet ef migrations remove

您仍然可以使用Update-Database命令。

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

然而,根据迁移的名称判断,我假设这是第一次迁移,因此该命令可能不起作用。您应该能够从数据库中的__MigrationHistory表中删除该条目,然后再次运行Remove-Migration命令。您还可以删除迁移文件并重新开始。

因为这个问题本身涉及到第一次迁移,所以这个答案适用于任何正在寻找恢复上次迁移的方法的人,因为大多数答案不包括替代场景(对我们大多数人来说,这不是我们的第一次迁移,我们不能抹掉整个迁移历史)。这些命令用于包管理器控制台。

假设你的代码中有以下迁移:

20220110 _initialmigration 20200210 _goodmigration 20200210 _lastgoodmigration 20200210 _badmigration 20200210 _badmigrationagain

如果您已经像问题中那样将迁移应用到db

运行: Update-Database -Migration <Last_Good_Migration_Name> 注:名称不能有日期前缀。例如:Update-Database -Migration LastGoodMigration 然后删除坏迁移运行: Remove-Migration 注意:因为我们有两次糟糕的迁移,所以运行两次Remove Migration命令。

如果您还没有将迁移应用到db

运行:

Remove-Migration

在本例中,运行两次Remove Migration命令。

希望这能帮助到一些人。