当我在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命令。您还可以删除迁移文件并重新开始。

其他回答

一般来说,如果您正在使用Package Manager Console,删除特定迁移的正确方法是引用迁移的名称

Update-Database -Migration {Name of Migration} -Context {context}

另一种删除你根据文档应用的最后一个迁移的方法是使用命令:

dotnet ef migrations remove

这个命令应该从解决方案目录中的开发人员命令提示符(如何打开命令提示符)执行。

例如,如果你的应用程序名为“application”,在文件夹c:\Projects中。那么你的路径应该是:

C:\Projects\Application

移除迁移

如果上次迁移没有应用到数据库,则可以删除该迁移。使用下面的remove命令删除最后创建的迁移文件并恢复模型快照。

包管理控制台

点> remove-migration

CLI> dotnet ef迁移删除

上面的命令将删除最后一次迁移,并将模型快照恢复到前一次迁移。请注意,如果迁移已经应用到数据库,那么它将抛出以下异常。

迁移已经应用到数据库。恢复并重试。如果迁移已经应用到其他数据库,请考虑使用新的迁移恢复其更改。

恢复迁移

假设您更改了域类,并使用add-migration命令创建了名为MySecondMigration的第二次迁移,并使用Update命令将此迁移应用到数据库。但是,由于某种原因,您希望将数据库恢复到以前的状态。在这种情况下,使用update-database命令将数据库恢复到指定的先前迁移快照。

包管理控制台

更新数据库MyFirstMigration

CLI

. net ef数据库更新MyFirstMigration。

上面的命令将根据名为MyFirstMigration的迁移恢复数据库,并删除应用于名为MySecondMigration的第二次迁移的所有更改。这也将从数据库中的__efmigrationhistory表中删除MySecondMigration条目。

注意:这不会删除与MySecondMigration相关的迁移文件。使用remove命令将它们从项目中删除。

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

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.

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

编码快乐!

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 <the-migration-you-want-to-recover>
dotnet ef migrations remove

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