实体框架$ C C首先迁移$觉得有变化,应该不会有 [英] Entity Framework Code First Migrations thinks there is a change that shouldn't be there

查看:138
本文介绍了实体框架$ C C首先迁移$觉得有变化,应该不会有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站和一个Windows服务,同时通过同一项目获得的实体框架数据上下文。我每次启动Windows服务,实体框架运行自动迁移,改变从NOT NULL的数据库列空的一个(​​没有​​其他更改)。该物业该列标记为[必填],以及网站(指向完全相同的DLL文件它的型号完全相同的版本),适当地认为,数据库应为该列NOT NULL。

I have a website and a windows service that both reference the same project to get an Entity Framework data context. Each time I start the windows service, Entity Framework runs an automatic migration that changes one of the database columns from NOT NULL to NULL (no other changes are made). The property for that column is marked as [Required], and the website (which points to the exact same version of the exact same DLL for its model), properly thinks that the database should be NOT NULL for that column.

我试图禁用自动迁移和,如预期的服务的话,崩溃,因为它说,该数据模型有这样的需要挂起更改得到应用。

I tried disabling automatic migrations and, as expected, the service then crashed because it said that the data model had pending changes that needed to be applied.

修改的我发现了更多的信息.​​..看来,这种情况正在发生,因为我的财产无论[必填]和[AllowHtml]属性。当我删除了[AllowHtml]属性,它并没有发生。所以,我的问题归结为:1)是预期[AllowHtml]不会与[必填]的工作行为,以及2)怎么可能,当Web服务使用这个code这只会发生,不是当网站使用了code?看来,网络服务完全忽略[必填]它认为,当[AllowHtml]。

Edit I've found out a bit more info... it seems that this is happening because I have both [Required] and [AllowHtml] attributes on the property. When I removed the [AllowHtml] attribute, it didn't happen. So, my question comes down to: 1) is it expected behavior that [AllowHtml] would not work with [Required], and 2) how is it possible that this would only happen when the web service uses this code, and not when the website uses the code? It seems that the web service completely ignores [Required] when it sees [AllowHtml].

我使用的是EF 5。

推荐答案

我有确切的问题... 对于挂起的更改我不得不添加三线插入启动我的Global.asax文件,这就是它的样子:

I had the exact problems... For the pending changes I had to add three lines into my global.asax file on startup, this is what it looks like:

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        AuthConfig.RegisterOpenAuth();
        Database.SetInitializer<EntityContext>(null);
        EntityContext context = new EntityContext();
        context.Database.Initialize(true);
    }

至于,你使用MVC [Allowhtml]? ...据我可以告诉[AllowHtml]属于System.Web.Mvc命名空间:的http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.98).aspx 。 AllowHtml意欲使用仅用于一种类型的模型结合。它们不是用于表单,查询字符串或的FormCollection模型绑定。 http://forums.asp.net/t/1645209.aspx 如果一个属性标有AllowHtmlAttribute属性,ASP.NET MVC框架跳过验证模型的过程中属性绑定,从而rignores你[必需]属性...

As for the [Allowhtml], are you using mvc? ...as far as I can tell [AllowHtml] belongs to the System.Web.Mvc namespace: http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.98).aspx . AllowHtml is intended to use only for Model binding of a type. They are not intended for Form, QueryString or FormCollection model binding. http://forums.asp.net/t/1645209.aspx If a property is marked with the AllowHtmlAttribute attribute, the ASP.NET MVC framework skips validation for that property during model binding, thus rignores your [Required] attribute...

的原因,列从NOT NULL更改为NULL是因为[必需]属性覆盖数据库模式规则,​​允许数据字段为空。 <一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx

The reason that the column is changing from NOT NULL to NULL is because the [Required] attribute overrides the database schema rule that allows a data field to be empty. http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx

希望这有助于...

这篇关于实体框架$ C C首先迁移$觉得有变化,应该不会有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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