The model backing the 'XXX' context has changed 错误
来源:互联网 发布:北京java培训中心 编辑:程序博客网 时间:2024/06/05 05:18
使用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就可以了。
- The model backing the 'XXX' context has changed 错误
- Additional information: The model backing the 'StuInfoDBContext' context has changed since the datab
- MVC中“The model backing the '***Context' context has changed since the database was created. ”的问题处理
- EF Code First(提示The model backing the '***' context has changed since the database was created.)
- EntityFramework错误:The entity type XXX is not part of the model for the current context
- The content of the adapter has changed but ListView did not receive a notification错误分析
- Adapter报错:The content of the adapter has changed
- The content of the adapter has changed解决方法
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- The content of the adapter has changed but ListView
- Adapter报错:The content of the adapter has changed
- Canvas学习笔记(一)线条和圆弧
- linux:ls、ls -l、ls -al区别
- android ScrollView 滚动到顶部
- gcc预处理阶段
- Hibernate配置文件与映射文件详解
- The model backing the 'XXX' context has changed 错误
- 前置和后置++
- Eclipse项目上传码云
- 【转载】一位资深程序员大牛给予Java初学者的学习路线建议
- Test
- python做音频格式转换
- Appium输入慢的原因分析
- WordPress 状态信息统计代码
- springMVC学习笔记