Entity Framework Code First Migrations thinks there is a change that shouldn't be there

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 20:55:18

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);
    }

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...

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

Hope this helps...

In the EF project, have you tried disabling automatic migrations, and then forcing EF to not keep the model and db in sync?

In your global.asax file, add:

Database.SetInitializer<dbContext>(null);

This will remove the message that pending migrations need to be applied

Cheers, Bartek

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!