[英]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:该怎么办:
You have now reset your migrations and may continue with normal migrations.您现在已重置迁移,可以继续进行正常迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.