我已经搞砸了我的迁移,我在初始迁移上使用了IgnoreChanges,但现在我想删除所有的迁移,并从具有所有逻辑的初始迁移开始。

当我删除文件夹中的迁移并尝试添加- migration时,它不会生成一个完整的文件(它是空的-因为我没有做任何更改,但现在删除了,迁移)。

是否有禁用-迁移命令,以便我可以重新运行启用-迁移?


当前回答

如何

Update-Database –TargetMigration: $InitialDatabase

在包管理器控制台?它应该将所有更新重置到非常早期的状态。

参考链接:代码优先迁移-迁移到特定版本(包括降级)

其他回答

在实体框架核心。

从migrations文件夹中删除所有文件。 输入控制台 Dotnet ef数据库drop -f -v dotnet ef迁移添加初始化 更新dotnetef数据库 (或用于包管理器控制台) Drop-Database -Force -Verbose Add-Migration初始 - database就

UPD:只有在不关心当前持久化数据时才这样做。如果有,就用Greg Gum的答案

你需要:

删除状态:删除项目中的迁移文件夹;和 删除数据库中的__MigrationHistory表(可能在系统表下);然后 在包管理器控制台中执行以下命令: Enable-Migrations -EnableAutomaticMigrations -Force 使用-EnableAutomaticMigrations或不使用-EnableAutomaticMigrations 最后,你可以运行: Add-Migration初始

问题:你搞砸了你的迁移,你想重置它而不删除现有的表。

问题:您不能重置数据库中现有表的迁移,因为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.

现在您已经重置了迁移,可以继续进行正常的迁移。

我的问题是手动删除了Migrations文件夹。我这样做是因为我想备份内容,所以我只是将文件夹拖出项目。后来我修复了这个问题,把它放回去(在做了备份副本之后),然后在解决方案资源管理器中右键单击它并从弹出菜单中选择删除,从而删除了Migrations文件夹。

考虑到当我们在。net Core中搜索EF时仍然会出现这个问题,我将在这里发布我的答案(因为它一直困扰着我)。注意,EF 6 .NET版本有一些微妙之处(没有初始命令,你将需要删除“快照”文件)

(在。net Core 2.1中测试)

以下是步骤:

删除_efmigrationhistory表。 在整个解决方案中搜索名称中包含Snapshot的文件,例如ApplicationDbContextSnapshot.cs,然后删除它们。 重新构建解决方案 执行Add-Migration InitialMigration命令

请注意: 必须删除所有快照文件。我花了无数个小时删除数据库……如果您不这样做,这将生成一个空迁移。

此外,在#3中,您可以随意命名您的迁移。

以下是一些额外的资源: asp.net CORE迁移生成空

重置实体框架7迁移