The model backing the 'XXX' context has changed 错误
2017-05-27 10:18
369 查看
使用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就可以了。
网上给出的解决方案一般是:
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就可以了。
相关文章推荐
- 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. ”的问题处理
- The model backing the <Database> 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
- Model backing a DB Context has changed; Consider Code First Migrations
- android The content of the adapter has changed but ListView did not receive a notification 错误的解决方案
- The model backing the 'ProductContext' context has changed since the database was created. EF6
- anroid开发遇到问题:跳转出现错误 the application xxx has stopped unexpectedly
- The content of the adapter has changed but ListView did not receive a notification 错误的解决方案
- The ECDSA host key for XXX has changed
- The content of the adapter has changed but ListView did not receive a notification 错误的解决方案
- 解决 PowerDesigner 错误 The generation has been cancelled because errors have been found by the check model.
- (ListView使用错误)The content of the adapter has changed but ListView did not receive anotification
- 解决连接 ssh 的时候 ,报 The server key has changed 错误
- 使用pod install发生的错误:[!] The 'XXX' target has frameworks with conflicting names: ummo
- Unity 错误(1)——The name `xxx' does not exist in the current context
- The content of the adapter has changed but ListView did not receive a notification 错误的解决方案
- mybatis 引用对象属性映射错误 or could not be found for the javaType (xxx.model) : jdbcType (null) combination.
- SSH登陆错误 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!