The model backing the 'XXX' context has changed 错误
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 错误
- 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就可以了。
The model backing the 'XXX' context has changed 错误的更多相关文章
- Model backing a DB Context has changed; Consider Code First Migrations
Model增加一个字段,数据库对应的也手动添加了字段但是运行时报错 The model backing the 'TopLogDbContext' context has changed since ...
- 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 ...
- 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 ...
- The model backing has changed
其他信息: The model backing the 'WebTopFormContext' context has changed since the database was created. ...
- Entity Framework : The model backing the '' context has changed since the database was created
1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the '' cont ...
- 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 ...
- ios 关于[xxx timeIntervalSinceNow]出现EXC_BAD_ACCESS错误的解决办法
[xxx timeIntervalSinceNow]出现EXC_BAD_ACCESS错误的主要原因是之前的[NSDate date]返回一个autoreleased的NSdata,其被释放掉 解决方法 ...
- The entity type <type> is not part of the model for the current context
这是在网站里遇到的一个错误,自动生成的不能手动添加, reference: http://stackoverflow.com/questions/19695545/the-entity-type-xx ...
- 关于Hibernate XXX is not mapped 错误
我的实体类是这么配置的 @Entity(name="EntityName") //必须,name为可选,对应数据库中一的个表 就会出现 XXX is not mapped. ...
随机推荐
- jquery简单ajax示例_读取json文件数据
来自于<jquery权威指南> -------------------------------------- 点击button后,获取到json文件数据,显示如下: Json文件: [ { ...
- fitting 方法的异常值过滤
training = pd.DataFrame({'x':[3,6,9,15,300, 20,85]}). 原始数据training_fitting = pd.DataFrame({'x':[4,7 ...
- JS中变量和常量的定义
var只能声明一个变量,这个变量可以保存任何数据类型的值 ES6之前并没有定义声明常量的方式,ES6标准中引入了新的关键字const来定义常量 使用const定义常量后,常量无法改变 const a= ...
- EXCEL 导入 R 的几种方法 R—readr和readxl包
导入Excel数据至R语言的几种方法 如有如下Excel数据源,如何将数据导入R语言呢?今天主要来介绍几种常见的方法: 一.使用剪贴板,然后使用read.table函数: 首先选择Excel中的数据源 ...
- python nose的html报告优化
用的是nose的nose-html-reporting (0.2.3)插件生成报告.用了bootstrap前端框架,加入了开始时间和计算持续时间,及其本地化. 优化后的显示效果: 代码地址
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...
- idea 启动报错问题
Artifact SpiderServer:war exploded: Error during artifact deployment. See server log for details. 1. ...
- zuluCryt cli howto
1.解锁卷的命令. zuluCrypt-cli -o -d /dev/sdc1 -m blabla -e ro -f /home/keyFile zuluCrypt-cli -o -d /dev/sd ...
- 10.13DOM中document--文档1找到元素的方法,还有元素内容属性
今天讲了js的组成部分中的第二组成部分(DOM),DOM包括以下内容: window -- 窗口 ...
- System.Timers.Timer用法
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(object se ...