https://blog.csdn.net/hit_why/article/details/72778785

https://blog.csdn.net/hit_why/article/details/72778785

The model backing the 'XXX' context has changed 错误

原创 2017年05月27日 10:18:47
  • 271

使用Entity FrameWork的Code First时,当改变模型的结构时,运行程序会出现The model backing the 'XXX' context has changed 错误,因为这时模型结构与数据库中相应表的结构不一致,不能直接进行修改。

网上给出的解决方案一般是:

1-->简单删除数据库(包括其中的所有数据),然后让Code First使用默认规则(无数据库--创建新数据库)来使用更新模型创建数据库.

这显然非常麻烦。

2-->Code First在遇到模型变化时有一套初始化策略可供使用,该行为删除数据库并重建,默认是封装在名为CreateDatabaseIfNotExists的 类中,你可告知正在执行中的程序(本处是指控制台程序)使用哪个策略。

这样还是要删除并重建数据库,代价是很大的。

3-->需要在Global.asax中的Application_Start方法中加入:

Database.SetInitializer<Models.XXXXDBContext>(null);

这种方式没试过,应该是针对于MVC框架的。

这里我给出一种方案,网上也可以搜到:

1. 打开工具-->NuGet包管理器-->程序包管理器控制台

2.在PM>后面输入Enable-Migrations -ContextTypeName DatabaseName(如果执行失败,就参考失败信信息重新输入,失败信息提示的非常明确;DatabaseName是你生成的数据库名字),然后你发现项目里面增加了一个Migrations文件夹,里面自动生成了一些代码,这些代码试根据你的模型和改变生成的

3. 设置Migrations文件夹下面的Configuration文件中的代码:AutomaticMigrationsEnabled=true;也就是将false改为true

4. 在PM>后面输入Add-Migration InitialCreate

5. 在PM>后面 Update-Database -Verbose 或者Update-Database -Verbose -Force强制改变数据库。

之后每次改变模型时,只需在PM>后面 输入Update-Database -Verbose -Force就可以了。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hit_why/article/details/72778785

The model backing the 'XXX' context has changed 错误的更多相关文章

  1. Model backing a DB Context has changed; Consider Code First Migrations

    Model增加一个字段,数据库对应的也手动添加了字段但是运行时报错 The model backing the 'TopLogDbContext' context has changed since ...

  2. EF框架CodeFirst the model backing the 'PModelEntities' context has changed since the database was created. Consider using Code First Migrations to update the database

    1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the 'Produc ...

  3. The model backing the <Database> context has changed since the database was created.

    Just found out the answer and thought of updating here. Just need to do the following. public class ...

  4. The model backing has changed

    其他信息: The model backing the 'WebTopFormContext' context has changed since the database was created. ...

  5. Entity Framework : The model backing the '' context has changed since the database was created

    1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the '' cont ...

  6. The entity type XXX is not part of the model for the current context.

    今天遇到了一个奇葩问题,虽然解决了,但还是一脸懵,先附赠一下别人的解决方案:https://www.cnblogs.com/zwjaaron/archive/2012/06/08/2541430.ht ...

  7. ios 关于[xxx timeIntervalSinceNow]出现EXC_BAD_ACCESS错误的解决办法

    [xxx timeIntervalSinceNow]出现EXC_BAD_ACCESS错误的主要原因是之前的[NSDate date]返回一个autoreleased的NSdata,其被释放掉 解决方法 ...

  8. The entity type <type> is not part of the model for the current context

    这是在网站里遇到的一个错误,自动生成的不能手动添加, reference: http://stackoverflow.com/questions/19695545/the-entity-type-xx ...

  9. 关于Hibernate XXX is not mapped 错误

    我的实体类是这么配置的 @Entity(name="EntityName")  //必须,name为可选,对应数据库中一的个表 就会出现 XXX is not mapped.   ...

随机推荐

  1. [C++] static member variable and static const member variable

    static member variable[可读可写] 可以通过指针间接修改变量的值 static const member variable[只读] 压根就不可以修改变量的值,会报错

  2. [SoapUI] 通过Groovy调用批处理文件.bat

    import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def proje ...

  3. scp 的时候提示WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    摘自:https://blog.csdn.net/haokele/article/details/72824847   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  4. springmvc框架简单搭建

    一.利用xml 配置 1.web.xml   <web-app version="2.4"     xmlns="http://java.sun.com/xml/n ...

  5. myeclipse便捷导包方式

    1.将spring框架的core导成如图的方式 2.选中项目右键--bulid path--进入buildpath 3.选择add library 4.选择user library 5.选择user ...

  6. idea中dependencies中总是有红色波浪线(缺少dependency)问题

    使用IDEA进行maven开发时,将新项目import进工作空间时,Maven Projects栏中的dependencies中总是有红色波浪线,如下图: 但是这些jar在我本地的maven仓库中实际 ...

  7. SQLServer学习-- Microsoft SQL Server 2008 Management Studio Express

    Microsoft SQL Server 2008 Management Studio Express is a free, integrated environment for accessing, ...

  8. Lua的文件IO操作

    Lua 标准库 - 输入输出处理(input and output facilities) 转载于:http://blog.csdn.net/duanxuyun 文本Tag: Lua [IT168 技 ...

  9. Jquery 欲绑定事件

    有时候,想提前给即将添加的的元素绑定事件,这时候使用on就不行了,利用事件的冒泡机制可以完成这个功能 Jquery 提供了delegate方法就是这样实现的. $("#schemaaccor ...

  10. Linux route命令 显示getnameinfo failed [UNKNOWN]

    Redhat配置IPv6以后,执行route显示getnameinfo failed [UNKNOWN] 修改/etc/hosts,增加主机名后,重启服务器,问题解决