简体   繁体   English

如何使用 EF Core 恢复迁移历史记录

[英]How to restore migration history with EF Core

Let's say I have a database which is created by three migrations.假设我有一个由三个迁移创建的数据库。 They can be seen in the table dbo._EFMigrationsHistory and the content could look like this:它们可以在表dbo._EFMigrationsHistory中看到,内容可能如下所示:

MigrationId                         ProductVersion
20200516181941_InitialMigration     3.1.4
20200517091058_AddedIndexes         3.1.4
20200605115456_IntroducedBreweries  3.1.4

Now let's say that I for some reason lose all of the table (or just a part of it).现在假设我由于某种原因失去了所有桌子(或只是其中的一部分)。 So suddenly the table look like this:突然,表格变成了这样:

MigrationId                         ProductVersion
20200517091058_AddedIndexes         3.1.4
20200605115456_IntroducedBreweries  3.1.4

And then I make a new migration, because i changed something in the database然后我进行了新的迁移,因为我更改了数据库中的某些内容

Add-Migration SomethingChanged

This will work fine, since we aren't talking to the database at this point.这将正常工作,因为此时我们没有与数据库交谈。 But if I try to update the database, like this但是如果我尝试更新数据库,就像这样

Update-Database

It will fail, because it it trying to re-run the InitialMigration (or at least so I assume).它会失败,因为它试图重新运行InitialMigration (或者至少我假设如此)。 So a common error is that the tables created in the InitialMigration cannot be created, because they already exist.所以一个常见的错误是InitialMigration中创建的表无法创建,因为它们已经存在。

During development you can simply nuke the database and/or migrations, but that's not a very viable strategy for production.在开发过程中,您可以简单地对数据库和/或迁移进行核对,但这不是一个非常可行的生产策略。 It can also be done with Script-Migration , which has a from/to option, allowing something like this:也可以使用Script-Migration来完成,它有一个 from/to 选项,允许这样的事情:

Script-Migration 20200517091058_AddedIndexes 20200605115456_IntroducedBreweries

But this doesn't seem to work on Update-Database (not in the documentation either).但这似乎不适用于Update-Database (也不是在文档中)。

Is there any (production safe) way to fix this, so that the migration history isn't broken for all eternity?是否有任何(生产安全)方法来解决这个问题,以便迁移历史不会永远被破坏?

The best way I can think of, is to manually insert the missing row(s) in the database, but this seems quite hack-ish...我能想到的最好方法是在数据库中手动插入丢失的行,但这似乎很hack-ish...

The Issue: You have mucked up your migrations and you would like to reset it without deleting your existing tables.问题:你搞砸了你的迁移,你想在不删除现有表的情况下重置它。

The Problem: You can't reset migrations with existing tables in the database as EF wants to create the tables from scratch.问题:您无法使用数据库中的现有表重置迁移,因为 EF 想要从头开始创建表。

What to do:该怎么办:

  1. Delete existing migrations from Migrations_History table.从 Migrations_History 表中删除现有迁移。
  2. Delete existing migrations from the Migrations Folder.从迁移文件夹中删除现有迁移。
  3. Run add-migration Reset.运行添加迁移重置。 This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.)这将在您的 Migration 文件夹中创建一个迁移,其中包括创建表(但它不会运行它,因此不会出错。)
  4. You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state.您现在需要在 MigrationHistory 表中创建初始行,以便 EF 拥有当前 state 的快照。 EF will do this if you apply a migration.如果您应用迁移,EF 将执行此操作。 However, you can't apply the migration that you just made as the tables already exist in your database.但是,您不能应用刚刚进行的迁移,因为数据库中已经存在表。 So go into the Migration and comment out all the code inside the "Up" method.所以 go 进入 Migration 并注释掉“Up”方法内的所有代码。
  5. Now run update-database.现在运行更新数据库。 It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.它将应用迁移(而不是实际更改数据库)并在 MigrationHistory 中创建快照行。

You have now reset your migrations and may continue with normal migrations.您现在已重置迁移,可以继续进行正常迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM