实体框架创建空迁移但坚持认为我的模型不同 [英] Entity Framework creates empty migration but insists that my model is different

查看:15
本文介绍了实体框架创建空迁移但坚持认为我的模型不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天是悲伤的一天.今天的第一件事是我看到 EF 异常说支持 'DomainContext' 上下文的模型自数据库创建以来发生了变化.".现在快午夜了,我仍然看到这个错误.我的职业生涯到此结束-(

Today is a sad day. First thing today I saw EF exception saying that "The model backing the 'DomainContext' context has changed since the database was created.". It is close to midnight and I still see this error. This is the end of my career -(

我很确定模型中没有任何变化,但出现了错误.我试过创建一个新的迁移,结果是空的:

I'm pretty sure nothing has changed in the model, yet the error appeared. I have tried creating a new migration, it came out empty:

public void Up()
{
}
public void Down()
{
}

应用此迁移没有任何好处 - 错误仍然存​​在.我使用了常见的建议将初始化程序设置为空:

Applying this migration did not do any good - the error persisted. I've used common suggestion to set the initialiser to be null:

Database.SetInitializer<DomainContext>(null);

当我访问数据库时,它使错误消失了.但这让我非常困扰 - 每当我尝试通过代码运行迁移时,我都会再次看到类似的错误:

And it made the error go away when I access the database. But this bothers me very much - whenever I try to run migrations through code, I see similar error again:

var configuration = new Migrations.Configuration();

configuration.TargetDatabase = new DbConnectionInfo("correct connection string", "System.Data.SqlClient");

var migrator = new DbMigrator(configuration);

migrator.Update(); // <<-- exception is thrown here

异常抛出如下所示:System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException : 无法更新数据库以匹配当前模型,因为存在待处理的更改并且自动迁移被禁用.将挂起的模型更改写入基于代码的迁移或启用自动迁移.将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 以启用自动迁移.

我已经更新到 EF 6.1(之前是 6.0.2),但这并没有什么不同.

I've updated to EF 6.1 (was on 6.0.2 before), but this made no difference.

让我困扰的另一件事是我可以通过 Nuget 控制台运行迁移:

Another thing that bothers me that I can run migrations through Nuget Console:

Update-Database

运行良好,没有任何问题.但是当我将数据库初始化程序设置为自动运行迁移时:

Runs fine and does not give any problems. But when I set DB initialiser to run migrations automatically:

var initializer = new MigrateDatabaseToLatestVersion<DomainContext, Migrations.Configuration>();
Database.SetInitializer(initializer);
var domainContext = new DomainContext();
domainContext.Database.Initialize(true); // <<-- this throws exception

无法更新数据库以匹配当前模型,因为存在待处理的更改并且自动迁移已禁用.将挂起的模型更改写入基于代码的迁移或启用自动迁移.将 DbMigrationsConfiguration.AutomaticMigrationsEnabled 设置为 true 以启用自动迁移.

真正的问题是,为什么 EF 在通过 Nuget 控制台和通过 Migrations DB-Initialiser 运行时对模型有不同的哈希值?如何找出不同之处(来自 db-state 的模型)?以及如何解决这个问题,这样我就不必使用 hacks(将 null 分配给 db-initaliser)?

The real question is why does EF has different hashes for models when running through Nuget console and through Migrations DB-Initialiser? How can I find out what is different (model from db-state)? And how to fix this, so I don't have to use hacks (assign null to db-initaliser)?

推荐答案

我的问题的原因是应用于其中一个模型的 [AllowHtml] 属性.在我将 MVC 更新到 5.1.1 并将 WebApi 更新到 2.1 之后,问题就开始发生了.

The cause to my problem was a [AllowHtml] attribute applied to one of the models. The trouble started to happen after I've updated MVC to 5.1.1 and WebApi to 2.1.

我已经从 EF-Model 中删除了该属性,并剥离并重新构建了数据库,问题就消失了.

I've removed that attribute from EF-Model and stripped and re-build the database and the problem was gone.

我写了一篇关于如何调试此类问题的博文:http://tech.trailmax.info/2014/03/inside_of_ef_migrations/

I've written up a blog post on how to debug this kind of problems: http://tech.trailmax.info/2014/03/inside_of_ef_migrations/

另外我认为这个 [AllowHtml] 属性是一个错误,我将创建一个可重现的解决方案并将错误报告提交给 EF-people.

Also I think this [AllowHtml] attribute is a bug, I'll create a reproducible solution and will submit the bug report to EF-people.

更新:我实际上永远无法重现该错误.类属性上的 [AllowHtml] 属性与它无关.这是一个神奇的故障.

Update: I actually could never reproduce the error. [AllowHtml] attribute on class properties had nothing to do with it. A magical glitch that was.

这篇关于实体框架创建空迁移但坚持认为我的模型不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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