【发布时间】:2013-04-02 13:22:23
【问题描述】:
我在一个项目(VS2012 Express 中的 ASP.NET MVC)中使用 Entity Framework (5.0) 已经有一段时间了。不过,现在我无法再添加迁移。
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.
我不知道这是否提供任何线索,但“无法...”文本显示为红色。
我尝试启用自动迁移(这没有任何意义,因为我试图将挂起的模型更改写入基于代码的迁移),这会导致在数据库中进行所需的迁移。然而,这不是我想要的,因为我在项目中没有迁移。
我已尝试删除数据库并重新创建数据库。重新创建了数据库(直到上一次迁移),但是当我尝试使用 Add-Migration 时,我仍然收到“无法更新..”错误。
编辑
我尝试了 -force 参数但没有任何区别。
我的配置类的内容(上次迁移后我没有改变任何东西):
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 中注释以下行时,我发现我能够进行添加迁移:
//Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
当我让上面的行保持活动状态并注释掉配置文件中的所有内容时,它仍然无法正常工作。 为什么 Database.SetInitializer 行会导致这种奇怪的行为?
【问题讨论】:
-
您能否发布您的配置文件以进行迁移?
-
你试过-force参数了吗?
-
我遇到了同样的问题,下面的 swapneel 的回答对我有用,尽管它有点极端。幸运的是我还处于起步阶段,所以没什么大不了的
标签: entity-framework