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

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

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


当前回答

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

(在。net Core 2.1中测试)

以下是步骤:

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

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

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

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

重置实体框架7迁移

其他回答

要解决这个问题,你需要:

删除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

如果您正确地完成了上述操作,那么这应该是成功的,现在您可以正常进行了。

对于 EFCore 6/7:

为了将所有更改合并到单个文件中,并将其应用到现有数据库中,同时保留所有数据(即在Production和其他开发中),以下步骤对我来说很有效:

Delete existing migrations from the Migrations Folder in your project. Delete the Context Snapshop files. I.e. ApplicationDbContextSnapshot.cs, DatabaseContextModelSnapshot.cs or similar. Rebuild your solution Run Add-Migration Initial. (Can be named how you like instead of Initial). This will create a migration in your Migration folder that includes creating the tables, but won't be applied yet. Open the generated Migration file and comment out all the code inside the "Up" method, so that it doesn't make any changes to all your existing tables. (This allows us to update the database in the correct format, without making any changes in the next step) Before the commented code within the Up function, add migrationBuilder.Sql("DELETE FROM [dbo].[__EFMigrationsHistory]"); to have the delete/truncate applied during migration

public partial class Initial : Migration
{
    public override void Up()
    {
        migrationBuilder.Sql("DELETE FROM [dbo].[__EFMigrationsHistory]");
        /*
          OTHER COMMENTED OUT CODE
        */
    }

    public override void Down()
    {
    }
}

Run Update-Database. It will apply the Migration and remove existing migration tracking entriers (while not actually changing the database schema) and create a snapshot row in __EFMigrationsHistory Submit this code and apply to your dev, staging and production databases as you normally would (Ensure the code is still commented out in the Up function, no changes will be applied, except a new entry created in __EFMigrationsHistory) Remove migrationBuilder.Sql("DELETE FROM [dbo].[__EFMigrationsHistory]"); and uncomment all the code from step 5 and submit back to source control. (This way new devs can create the db from scratch. If someone forgot to update their existing database, it'll error out and complain that the tables already exist, so they can just comment the Initial file out and apply.)

你需要:

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

在Net Core 3.0中:

我无法找到重置迁移的方法。

我还遇到了迁移中断的问题,这里提供的答案对我不起作用。我有一个。net Core 3.0 web API,在上个月的某个地方我直接编辑了数据库。是的,我做了一件非常非常糟糕的事。

这里建议的策略导致了包管理器控制台中的一些错误:

该名称的迁移已经存在 找不到快照 “力”不是一个公认的参数

当然,我可能错过了一个步骤,或者错过了清除正确的文件,但我发现有一些方法可以在不使用暴力的情况下清理这些文件:

从PMC中按名称(按创建的相反顺序)删除-迁移,直至并包括中断的迁移 Add-Migration创建一个新的迁移,它将是最后一次良好迁移到当前模式之间的增量

现在,当web API从一个空数据库启动时,它正确地创建了与实体模型匹配的所有表和属性。

HTH!

在实体框架核心。

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

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