c# - 如何使用 EF Core 在 ASP.NET Core 中取消应用迁移

标签 c# asp.net-core entity-framework-core .net-core visual-studio-2015

当我在 VS2015 中运行 PM> Remove-Migration -context BloggingContext 并使用 EF Core 运行 ASP.NET 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 Update 3。

最佳答案

使用:

命令行

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

包管理器控制台

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

例子:

PM> Update-Database MyInitialMigration

然后尝试删除上次迁移。

在没有数据库更新的情况下删除迁移不起作用,因为您已将更改应用到数据库。

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

关于c# - 如何使用 EF Core 在 ASP.NET Core 中取消应用迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192450/

相关文章:

c# - 如何使 Razor 页面仅在开发中可用?

asp.net-mvc - 小时总数 (HH :MM) in Linq

entity-framework - Scaffold-DbContext 和 dotnet-ef-dbcontext-scaffold 的区别

entity-framework-core - 我无法运行迁移(使用 .net core 3.0 和 Entity Framework )

c# - 从 SQL 表构建 C# dll

c# - 有没有办法知道一个实体是否已被 NHibernate 管理?

oauth-2.0 - IDX10503 : Signature validation failed

c# - SslStream:处理证书时发生未知错误

c# - 将文件添加到 Flurl 多部分 POST 请求时 IFormFileCollection 为空

asp.net-core - ASP.Net Core WebApi - 存储来自 ActionFilter 的值以在 Controller 中访问