我一直在项目中使用实体框架(5.0)一段时间(ASP.NET MVC中的VS2012 Express)。但是现在,我不再能够添加迁移。实体框架:添加迁移失败,无法更新数据库
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-Migratio ñ我仍然收到“无法更新..”错误。
编辑
我试图强制参数,但没有差异。
我的配置类的内容(我没有以前迁移后改变任何东西):
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行会导致这种奇怪的行为?
能否请您发表您的配置文件迁移? – IronMan84 2013-04-10 15:45:55
您是否尝试过-force参数? – 2013-04-10 20:25:43
我有同样的问题,swapneel的答案下面为我工作,虽然它有点极端。幸运的是,我处于开始阶段,所以它不是太大的交易 – 2013-07-16 18:46:40