如何使用 EF Core 在 ASP.NET Core 中取消应用迁移 [英] How to unapply a migration in ASP.NET Core with EF Core

查看:28
本文介绍了如何使用 EF Core 在 ASP.NET Core 中取消应用迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 PM> 时Remove-Migration -context BloggingContext in VS2015 with an ASP.NET Core project using EF Core 我收到以下错误:

System.InvalidOperationException:迁移20160703192724_MyFirstMigration"已应用于数据库.取消应用并重试.如果迁移已应用于其他数据库,请考虑使用新迁移恢复其更改.在 Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force)在 Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force)在 Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0()在 Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)在 Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)迁移20160703192724_MyFirstMigration"已应用于数据库.取消应用并重试.如果迁移已应用于其他数据库,请考虑使用新迁移恢复其更改.

我怎样才能取消应用它?我使用的是最新版本的 ASP.NET Core 1.0、EF Core 和 VS2015 Update 3.

解决方案

使用:

CLI

<代码>>dotnet ef 数据库更新

包管理器控制台

PM>Update-Database

示例:

PM>更新-数据库 MyInitialMigration

然后尝试删除上次迁移.

在不更新数据库的情况下删除迁移不起作用,因为您对数据库应用了更改.

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

When I run PM> Remove-Migration -context BloggingContext in VS2015 with an ASP.NET Core project using EF Core I get the following error:

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.

How can I unapply it? I'm using latest release of ASP.NET Core 1.0, EF Core, and VS2015 Update 3.

解决方案

Use:

CLI

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

Package Manager Console

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

Example:

PM> Update-Database MyInitialMigration

Then try to remove last migration.

Removing migration without database update doesn't work because you applied changes to database.

If using PMC, Try: PM> update-database 0 This will wipe the database and allow you to remove the Migration Snapshot on your Solution

这篇关于如何使用 EF Core 在 ASP.NET Core 中取消应用迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆