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. office 2003和office 2013同时安装使用的问题

    电脑上同时安装了Office 2003和Office 2007/2010,先打开Word 2003,然后再打开Word 2010,总会弹出安装配置界面,反之亦然.  解决方法:使用快捷键Win+R打开 ...

  2. 【MVC】ASP.NET MVC 请求生命周期

    当一个asp.net mvc应用程序提出请求,为了响应请求,包含一些请求执行流程步骤! 在asp.net mvc应用程序Http request和Http response 过程中,主要包含8个步骤: ...

  3. Git入门详解

    查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git checkout -b ...

  4. (笔记)安装npm需要更改代理kneesocks 1081 1080

  5. leetcode 1

    题目: 最开始采用暴力解法,两个for循环遍历所有组合形式,时间复杂度为O(n2),代码省略. 进一步学习,采用hash表存储,空间换时间,时间复杂度为O(n),空间复杂度为O(n); 将数组放入ha ...

  6. php遍历mysql资源

    mysql_query('use test');             //选库 mysql_query('set names utf8');        //编码 $sql="sele ...

  7. 浏览器内置Console函数使用详解

    浏览器内置Console函数比较好用:Chrome 和 FireFox(Firebug插件) 利用此功能可以像直接在面板里面运行JS一样(写法不同而已) 一.显示信息的命令 Firebug内置一个co ...

  8. Knockout.Js官网学习(html绑定、css绑定)

    Html绑定 html绑定到DOM元素上,使得该元素显示的HTML值为你绑定的参数.如果在你的view model里声明HTML标记并且render的话,那非常有用. 简单示例 <div dat ...

  9. Navicat Premium 11 For Mac 注册机

    http://mac.pcbeta.com/thread-138357-1-1.html

  10. 【转】mysql字符串函数

    对于针对字符串位置的操作,第一个位置被标记为1(即:第一个字母索引为1). ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL ...