我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。
当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。
是否有禁用-迁移命令,以便我可以重新运行启用-迁移?
我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。
当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。
是否有禁用-迁移命令,以便我可以重新运行启用-迁移?
当前回答
要解决这个问题,你需要:
删除Migrations文件夹中的所有*.cs文件。 删除数据库中的_MigrationHistory表 执行Enable-Migrations -EnableAutomaticMigrations -Force命令 执行Add-Migration Reset命令
然后,在公共部分类Reset: DbMigration类中,你需要注释所有现有的和当前的table:
public override void Up()
{
// CreateTable(
// "dbo.<EXISTING TABLE NAME IN DATABASE>
// ...
// }
...
}
如果你错过了这一点,一切都将失败,你必须重新开始!
现在运行Update-Database -verbose
如果您正确地完成了上述操作,那么这应该是成功的,现在您可以正常进行了。
其他回答
如何
Update-Database –TargetMigration: $InitialDatabase
在包管理器控制台?它应该将所有更新重置到非常早期的状态。
参考链接:代码优先迁移-迁移到特定版本(包括降级)
问题:你搞砸了你的迁移,你想重置它而不删除现有的表。
问题:您不能重置数据库中现有表的迁移,因为EF希望从头创建表。
应对方法:
Delete existing migrations from Migrations_History table. Delete existing migrations from the Migrations Folder. 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.) You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. 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. Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory.
现在您已经重置了迁移,可以继续进行正常的迁移。
在EntityFramework 6中,请尝试:
Add-Migration Initial
以便更新初始迁移文件。
在Net Core 3.0中:
我无法找到重置迁移的方法。
我还遇到了迁移中断的问题,这里提供的答案对我不起作用。我有一个。net Core 3.0 web API,在上个月的某个地方我直接编辑了数据库。是的,我做了一件非常非常糟糕的事。
这里建议的策略导致了包管理器控制台中的一些错误:
该名称的迁移已经存在 找不到快照 “力”不是一个公认的参数
当然,我可能错过了一个步骤,或者错过了清除正确的文件,但我发现有一些方法可以在不使用暴力的情况下清理这些文件:
从PMC中按名称(按创建的相反顺序)删除-迁移,直至并包括中断的迁移 Add-Migration创建一个新的迁移,它将是最后一次良好迁移到当前模式之间的增量
现在,当web API从一个空数据库启动时,它正确地创建了与实体模型匹配的所有表和属性。
HTH!
我的问题是手动删除了Migrations文件夹。我这样做是因为我想备份内容,所以我只是将文件夹拖出项目。后来我修复了这个问题,把它放回去(在做了备份副本之后),然后在解决方案资源管理器中右键单击它并从弹出菜单中选择删除,从而删除了Migrations文件夹。