如何完全重置所有Entity Framework 6迁移历史记录?(僵尸迁移!)

O'R*_*ney 7 .net ef-migrations entity-framework-6

精简版:

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

长版:

我们正在使用Entity Framework 6,针对SQL Server 2012.它是一个MVC webapp,我们正在使用基于代码的迁移,此时我们有一个适度的迁移历史.我们在启动时运行迁移:

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

        var configuration = new Configuration();
        var migrator = new DbMigrator(configuration);
        migrator.Update();
    }
}
Run Code Online (Sandbox Code Playgroud)

在几台PC上它工作正常,但在一台 PC上,我遇到了一些问题,它们在某些方面似乎不同步.每当应用程序运行时,它都会抱怨有待更改.

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: 
Run Code Online (Sandbox Code Playgroud)

但是运行add-migration显示没有任何更改,update-database什么都不做.

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

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

他们来自哪里?

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

Configuration.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" }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

mil*_*onb 5

根据评论链和您似乎尝试过的内容,我建议采取以下行动方案.请注意我假设您有某种形式的代码存储(如果不是请备份您的代码).

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

如果这不能修复您的项目,那么我的猜测是Visual Studio安装的问题(重新安装,我希望不会).

祝好运.