The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

How to solve the error The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

1.今天查询数据的出现对象已经被释放,然后字段属性为null。我下了断点检查了一下数据,是有值的。

2.我发现问题出在我访问了另一个实体的子实体,也就是一个表的字表数据,EF是ORM技术所以用面向对象的方式去访问这个子对象的属性。就报错了。

上网找了一下,看到老外这样说的

Company should be lazy-loaded in your case. But your entity now in detached state (context which loaded KBrand entity now disposed. So, when you are trying to access Company property, Entity Framework tries to use disposed context to make query to server. That gives you exception.

提供了解决方案

RSPDbContext c = new RSPDbContext();
KBrand co = item.Tag as KBrand;
c.KBrands.Attach(co);
// use co.Company OR you need to use eager loading, to have Company already loaded. Something like this when you getting items:
RSPDbContext db = new RSPDbContext();
var brands = db.KBrands.Include(b => b.Company).ToList();
// assign brands to listbox

具体的原因说起来有点麻烦。涉及到POCO。

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.的更多相关文章

  1. to disable the entity lazy load, The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

    The ObjectContext instance has been disposed and can no longer be used for operations that require a ...

  2. C# Entity Framework The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

    The ObjectContext instance has been disposed and can no longer be used for operations that require a ...

  3. EF架构获取数据时报错:The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. Do you want to correct the value?

    场景: EF底层,获取完主表,点击按钮,添加主表,字表内容时,报以上错误 解决方案: 在EF文件的空白处右键--属性,将“应用延迟加载”,改为False

  4. Entity Framework 数据并发访问错误原因分析与系统架构优化

    博客地址 http://blog.csdn.net/foxdave 本文主要记录近两天针对项目发生的数据访问问题的分析研究过程与系统架构优化,我喜欢说通俗的白话,高手轻拍 1. 发现问题 系统新模块上 ...

  5. EF 5 最佳实践白皮书

    Performance Considerations for Entity Framework 5 By David Obando, Eric Dettinger and others Publish ...

  6. EntityFrameWork使用过程问题总结

    1.记录上次遇到个一个问题. (1).vs2013中的EntityFramework不能识别odp11,所以在用ef的时候 ,要换vs2012 (2).opd12不能识别Oracle  9i(所以这个 ...

  7. Thrift搭建分布式微服务(三)

    第一篇 <连接配置> 第二篇 <连接池> 第三篇 标准通信 一.TCP的连接是无状态的,怎样知道我的请求被服务端接受并且正确执行了呢? 我的解决方案是使用自己定义的标准输入输出 ...

  8. https那些事儿

    (一)SSL/TLS协议运行机制的概述 一.作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. (1) 窃听风险(eavesdropping):第三方可以获 ...

  9. IDispose(), Finalize()

    C#  using 用法: 1. 作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型.(见例1-1) 2. 作为语句,用于定义一个范围,在此范围的末尾将释放对象.(见例1-2) (例子1- ...

随机推荐

  1. jQuery插件开发方式

    一.jQuery扩展 1.$.extend(object) 类似于.Net的扩展方法,用于扩展jQuery.然后就可以用$.的方式调用. $(function(){ $.extend({ fun1: ...

  2. Something wrong with FTK's index search results

    My friend she told me last week that FTK could not "see" keywords in a plain text files wh ...

  3. ListCtrl接受拖动文件

    [引言] 拖放操作在电脑中很常用,例如我们经常复制文件就可以按住ctrl键不放,然后再拖到另外一个窗口中,或者,可以把一个WORD文档直接拖动到WORD窗口即可打开,以前我使用过VB,里面直接有Ole ...

  4. WP8 调用特定API权限不足

    1.在解决方案中依次打开  Properties -> WMAppManifest.xml 2.点击功能 3.在左侧功能列表中勾选想要的功能权限.完毕

  5. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

  6. ChainOfResponsibility

    #include <iostream> using namespace std; class Chain { public: bool Handle() { return false; } ...

  7. jquery 仿手机屏幕切换界面效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Linux命令学习---目录

    一.文件相关命令 1.文件显示 1)tail,head,more,less,cat,nl

  9. Mongodb添加地理位置索引

    1.同步站点信息到mongo中(支持mysql.sqlserver数据同步) 2.在Collections文件夹下所在文档右键,在菜单中选择Add Index, 3.然后进行数据查询{ "m ...

  10. 发布阿里云OSS for phpcmsV9整合教程

    说明:这个算不上是插件,因为没有安装包,需要手工修改代码. 还有一点就是后台发布文章时上传的附件还是会保存在你的服务器上,基于以下原因: 1.个人的需求是前台页面需要使用thumb函数生成多个缩略图大 ...