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


当前回答

Use:

CLI

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

包管理控制台

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

例子:

PM>更新数据库MyInitialMigration

然后尝试删除上次迁移。

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

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

其他回答

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

update-database 0

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

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

dotnet ef database update 0
dotnet ef migrations remove

在包管理器控制台:

- database就Your_Migration_You_Want_To_Revert_To

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

在迁移已经应用到数据库后“取消”最近的迁移:

Open the SQL Server Object Explorer (View -> "SQL Server Object Explorer") Navigate to the database that is linked to your project by expanding the small triangles to the side. Expand "Tables" Find the table named "dbo._EFMigrationsHistory". Right click on it and select "View Data" to see the table entries in Visual Studio. Delete the row corresponding to your migration that you want to unapply (Say "yes" to the warning, if prompted). Run "dotnet ef migrations remove" again in the command window in the directory that has the project.json file. Alternatively, run "Remove-Migration" command in the package manager console.

希望这有助于并适用于项目中的任何迁移…我只测试了最近的迁移…

编码快乐!

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

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

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