本文翻译自:Reset Entity-Framework Migrations
I've mucked up my migrations, I used IgnoreChanges
on the initial migration, but now I want to delete all my migrations and start with an initial migration with all of the logic. 我已经对迁移进行了处理,在初始迁移中使用了IgnoreChanges
,但是现在我想删除所有迁移,并从所有逻辑开始进行初始迁移。
When I delete the migrations in the folder and try and Add-Migration
it doesn't generate a full file (it's empty - because I haven't made any changes since my last, but now deleted, migration). 当我删除文件夹中的迁移并尝试执行“ Add-Migration
它不会生成完整的文件(该文件为空-因为自上次迁移以来,我没有进行任何更改,但是现在删除了该迁移)。
Is there any Disable-Migrations command, so I can rerun Enable-Migrations
? 是否有任何Disable-Migrations命令,所以我可以重新运行Enable-Migrations
?
#1楼
参考:https://stackoom.com/question/N0lb/重置实体框架迁移
#2楼
You need to : 你需要 :
- Delete the state: Delete the migrations folder in your project; 删除状态:删除项目中的migrations文件夹; And 和
- Delete the
__MigrationHistory
table in your database (may be under system tables); 删除数据库中的__MigrationHistory
表(可能在系统表下); Then 然后 Run the following command in the Package Manager Console: 在程序包管理器控制台中运行以下命令:
Enable-Migrations -EnableAutomaticMigrations -Force
Use with or without
-EnableAutomaticMigrations
与-EnableAutomaticMigrations
一起使用或不-EnableAutomaticMigrations
And finally, you can run: 最后,您可以运行:
Add-Migration Initial
#3楼
My problem turned out to be that I manually removed the Migrations folder. 我的问题原来是我手动删除了Migrations文件夹。 I did that because I wanted to back up the contents, so I simply dragged the folder out of the project. 我这样做是因为我想备份内容,所以我只是将文件夹拖出了项目。 I later fixed the problem by putting it back in (after making a backup copy), then removing the Migrations folder by right-clicking it in Solutions Explorer and choosing Delete from the popup menu. 稍后,我通过将其放回(制作完备份副本后),然后通过在“解决方案资源管理器”中右键单击它并从弹出菜单中选择“删除”来删除“迁移”文件夹来解决该问题。
#4楼
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: 该怎么办:
Delete existing migrations from Migrations_History table. 从Migrations_History表中删除现有迁移。
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. 现在,您需要在MigrationHistory表中创建初始行,以便EF拥有当前状态的快照。 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. 因此,进入“迁移”并注释掉“ Up”方法中的所有代码。
Now run update-database. 现在运行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. 现在,您已重置了迁移,并可以继续进行正常迁移。
#5楼
How about 怎么样
Update-Database –TargetMigration: $InitialDatabase
in Package Manager Console? 在Package Manager控制台中? It should reset all updates to its very early state. 它应该将所有更新重置为非常早的状态。
Reference link: Code First Migrations - Migrating to a Specific Version (Including Downgrade) 参考链接: 代码优先迁移-迁移到特定版本(包括降级)
#6楼
In EntityFramework 6 please try: 在EntityFramework 6中,请尝试:
Add-Migration Initial
in order to update the initial migration file. 为了更新初始迁移文件。