首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将'Model.Entities‘类型的对象强制转换为'System.Data.Objects.ObjectContext’类型

无法将'Model.Entities‘类型的对象强制转换为'System.Data.Objects.ObjectContext’类型
EN

Stack Overflow用户
提问于 2014-05-19 04:57:38
回答 1查看 2.2K关注 0票数 0

MVC3 EF5.它运行得很好。但是当我从数据库更新模型时,构建成功了,但我在最后一行得到了上面的异常。我最后一次从数据库更新是在2-3个月前,一切都很好。

代码语言:javascript
复制
 public static ObjectContext GetContext()
 {
     Assembly testAssembly = Assembly.GetExecutingAssembly();
     Type calcType = testAssembly.GetType("Model.Entities");
     return (ObjectContext)Activator.CreateInstance(calcType);
 }
EN

回答 1

Stack Overflow用户

发布于 2014-05-20 11:07:31

较新版本的实体框架提供了DbContext,而不是<= EF 4.0的ObjectContext。但是,仍然可以通过IObjectContextAdapter返回对ObjectContext的引用

代码语言:javascript
复制
 Assembly testAssembly = Assembly.GetExecutingAssembly();
 Type calcType = testAssembly.GetType("Model.Entities");
 var entities = (DbContext)(Activator.CreateInstance(calcType));
 return ((IObjectContextAdapter)entities).ObjectContext;

然而,就我个人而言,我会考虑升级您的代码以返回DbContext,因为它更高级。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23726773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档