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

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

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

其他回答

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

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

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

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

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

1.找到表“dbo”。_efmigrationshhistory”,然后删除要删除的迁移记录。 2. 在PM(包管理控制台)运行“remove-migration”。 对我有用。

在包管理器控制台:

- database就Your_Migration_You_Want_To_Revert_To

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

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

dotnet ef database update {migration_name}

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

dotnet ef migrations list