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

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

问题描述

今天是个悲伤的日子。第一件事,今天我看到了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()
{
}

应用这种迁移并没有任何好处 - 错误坚持我用共同建议设置初始化器为null。

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启用自动迁移。

我已经更新到6.1 EF(是在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控制台,并通过迁移运行时DB-初始化器?我怎样才能找出什么不同(从DB-状态模型)?而如何解决这个问题,所以我没有用黑客(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-模式,属性和剥离并重新建立数据库,问题就消失了。

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-人的错误报告。

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天全站免登陆