实体框架:添加迁移失败,无法更新数据库 [英] Entity Framework: Add-Migration fails with Unable to update database

查看:280
本文介绍了实体框架:添加迁移失败,无法更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在项目(VS2012 Express中的ASP.NET MVC)中使用Entity Framework(5.0)一段时间了。但是,现在,我不再能够添加迁移。

I have been using Entity Framework (5.0) for a while now in a project (ASP.NET MVC in VS2012 Express). Right now, though, I am no longer able to add migrations.

PM > Add-Migration -projectName MyProject.DAL TestMigration
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.

我不知道这是否有任何线索,但显示无法...文本用红色表示。

I do not know if this gives any clue but the 'Unable to ..." text is displayed in red.

我试图启用自动迁移(这没有意义,因为我试图将未完成的模型更改写入基于代码的迁移)数据库中所需的迁移。但这不是我想要的,因为那时我在项目中没有迁移。

I have tried to enable automatic migration (which makes no sense as I am trying to write the pending model changes to a code-based migration) and that results in the required migration in the database. However this is not what I want because I then I do not have a migration in the project.

我尝试删除数据库重新创建数据库(直到上一次迁移),但是当我尝试使用Add-Migration时,仍然出现无法更新..错误。

I have tried to remove the database and recreate the database. The database is recreated (up to the previous migration) but when I then try to use the Add-Migration I still get the "Unable to update.." error.

编辑

我尝试了-force参数,但没有区别。

I tried the -force parameter but with no difference.

我的配置类的内容(在上一次迁移之后我没有做任何更改):

The contents of my configuration class (I did not change anything after the previous migration):

    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(Bekosense.DAL.Context.BekosenseContext context)
    {           
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageDrop);
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageCreate); 
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentDrop);
        context.Database.ExecuteSqlCommand(Properties.Resources.TriggerAlertMessageSentCreate); 
        context.Database.ExecuteSqlCommand(Properties.Resources.AddDbUsers);
    }

编辑2
当我在DbContext中注释以下行时,便能够执行添加迁移:

Edit 2 I found out that I am able to do an add-migration when I comment the following line out in my DbContext:

//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());

当我保持上面的行为活动状态并注释掉配置文件中的所有内容时,它仍然不会工作。
为什么Database.SetInitializer行会导致这种奇怪的行为?

when I leave the line above active and comment out everything in the Configuration file, it still won't work. Why is the Database.SetInitializer line causing this strange behaviour?

推荐答案

您可以重置实体框架来解决您的问题问题[但是请记住,它将使迁移进入默认状态]

You can reset the entity framework to solve your problem [But keep it mind it will bring the Migration to the default state]

注意:在执行迁移之前先进行备份以下

您需要删除当前状态:


  • 删除项目中的迁移文件夹

  • 删除数据库中的__MigrationHistory表(可能在系统表下)

您将在数据库的[App_Data文件夹下]找到__MigrationHistory表

You will find the __MigrationHistory table in your database [Under App_Data Folder]

然后在Package Manager控制台中运行以下命令:

Then run the following command in the Package Manager Console:

Enable-Migrations -EnableAutomaticMigrations -Force

使用或不使用-EnableAutomaticMigrations

Use with or without -EnableAutomaticMigrations

最后,您可以运行:

Add-Migration Initial

这也可能对您有帮助

这篇关于实体框架:添加迁移失败,无法更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆