【问题标题】:How to completely reset all Entity Framework 6 migration history? (Zombie migrations!)如何完全重置所有 Entity Framework 6 迁移历史? (僵尸迁移!)
【发布时间】:2015-08-27 21:11:21
【问题描述】:

短版:

在删除整个数据库(包括 __MigrationHistory)和解决方案中的所有迁移之后......不知何故,命名迁移正在某处找到并由 DbMigrator 应用!他们是从哪里来的???

加长版:

我们正在使用实体框架 6,针对 SQL Server 2012。它是一个 MVC Web 应用,我们正在使用基于代码的迁移,并且在这一点上我们有相当长的迁移历史。我们在启动时运行迁移:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...

        var configuration = new Configuration();
        var migrator = new DbMigrator(configuration);
        migrator.Update();
    }
}

在几台电脑上运行良好,但在一台电脑上,我遇到了一些问题,它似乎以某种方式不同步。每当应用程序运行时,它都会抱怨有待处理的更改。

System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException occurred
HResult=-2146233088
Message=Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
Source=EntityFramework
StackTrace:
     at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
     at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
     at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
     at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()
     at CVInsights.MvcApplication.Application_Start() in c:\Work\ClearVision\CVO\CVInsights\Global.asax.cs:line 20
InnerException: 

但是运行 add-migration 显示没有任何变化,并且 update-database 什么也没做。

所以,暂时放弃,我想完全“重置” EF 迁移。所以我删除了整个数据库(显然包括 __MigrationHistory 表)。我从 IDE Migrations 文件夹中删除了所有迁移。

现在...当我在这台新 PC 上启动我们的应用程序时...DbMigrator 仍然会从某个地方找到并应用一堆命名迁移!这不是我们目前拥有的完整列表(在我们的源代码管理中),但它是几周前某个时间点的列表。在应用了其中一些“僵尸迁移”后,它会抛出相同的旧异常“

他们到底是从哪里来的??

我觉得我一定是在做一些非常愚蠢的事情,或者 PC 从根本上搞砸了......

配置.cs:

public sealed class Configuration : DbMigrationsConfiguration<CloudDatabase.DAL.DatabaseContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(CloudDatabase.DAL.DatabaseContext context)
    {            

        context.Roles.AddOrUpdate(role => role.ID, // This is the primary key or the table we are adding to or updating.
            new Role() { ID = 1, Name = "A role" },
            new Role() { ID = 2, Name = "another role" },
            new Role() { ID = 3, Name = "third role" }
        );
    }
}

【问题讨论】:

  • 你确定你删除了所有的迁移(也许有些在不同的文件夹中)?在我看来,它正在应用您的部分迁移,然后尝试将您的上下文中的其余更改作为自动迁移应用。您是否有“基线”数据库,或者您的迁移是从空白数据库开始的?
  • 嗨,维桑。我认为我删除了所有迁移。我删除了 Migrations 文件夹中的所有 datetime_name.cs 文件。没有子文件夹。仍然有一个 configuration.cs,里面的内容不多。迁移从一个空白数据库开始 - 正如我所说,我删除了整个数据库以开始。自动迁移已关闭。 ( AutomaticMigrationsEnabled = false;在 configuration.cs 中)
  • 嗯,听起来你几乎涵盖了所有内容。您是否检查过使用 ildasm/reflector 运行的实际 dll 以确保它是正确的,而不是旧版本或其他什么?如果迁移日志没有告诉您有用的信息,也许您也可以尝试迁移日志(请参阅here)。否则我没有更多的想法。希望有人能够提供帮助。
  • 我添加了configuration.cs。
  • 您可以尝试查看thisthis 问题,看看它们是否没有帮助。

标签: .net entity-framework-6 entity-framework-migrations


【解决方案1】:

根据 cmets 链和您似乎已经尝试过的方法,我建议采取以下行动方案。请注意,我假设您有某种形式的代码存储(如果没有,请备份您的代码)。

  1. 删除有问题的数据库。
  2. 关闭 Visual Studio 的所有副本。
  3. 从您的解决方案文件夹中删除源代码(全部删除,不留下任何内容)。
  4. 从代码存储中提取代码副本(Pull、Get 等)
  5. 重建解决方案
  6. 运行update-database 命令并运行您的项目。

如果这不能解决您的项目,那么我猜测它可能是 Visual Studio 安装的问题(我希望不会重新安装)。

祝你好运。

【讨论】:

  • 这似乎解决了问题。迁移历史记录在我的工作文件夹中的“某处”,但我不知道在哪里。非常感谢!
  • 关闭和重新打开对我来说是这样的。
  • 确认,这解决了我遇到的同样问题。
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2017-04-06
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多