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


当前回答

为了在EF Core 1.0中取消迁移,使用以下命令:

dotnet ef database update {migration_name}

使用您希望保留更改的迁移的迁移名称。可以使用以下命令找到迁移的名称列表:

dotnet ef migrations list

其他回答

Use:

CLI

> . > dotnet ef database update <previous-migration-name> . bat . bat

包管理控制台

PM> Update-Database <previous-migration-name>

例子:

PM>更新数据库MyInitialMigration

然后尝试删除上次迁移。

删除迁移而不更新数据库无法工作,因为您对数据库应用了更改。

如果使用PMC,请尝试: PM>更新数据库0 这将擦除数据库,并允许您删除解决方案上的迁移快照

要取消应用特定的迁移:

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

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

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

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

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

dotnet ef database update 0
dotnet ef migrations remove

要恢复所有应用到DB的迁移,只需运行:

update-database 0

在migration目录中有可见的迁移文件时,应该多次运行Remove-Migration。该命令删除最近一次迁移,同时更新快照。