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 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.的更多相关文章
- 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 ...
- 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 ...
- 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
- Entity Framework 数据并发访问错误原因分析与系统架构优化
博客地址 http://blog.csdn.net/foxdave 本文主要记录近两天针对项目发生的数据访问问题的分析研究过程与系统架构优化,我喜欢说通俗的白话,高手轻拍 1. 发现问题 系统新模块上 ...
- EF 5 最佳实践白皮书
Performance Considerations for Entity Framework 5 By David Obando, Eric Dettinger and others Publish ...
- EntityFrameWork使用过程问题总结
1.记录上次遇到个一个问题. (1).vs2013中的EntityFramework不能识别odp11,所以在用ef的时候 ,要换vs2012 (2).opd12不能识别Oracle 9i(所以这个 ...
- Thrift搭建分布式微服务(三)
第一篇 <连接配置> 第二篇 <连接池> 第三篇 标准通信 一.TCP的连接是无状态的,怎样知道我的请求被服务端接受并且正确执行了呢? 我的解决方案是使用自己定义的标准输入输出 ...
- https那些事儿
(一)SSL/TLS协议运行机制的概述 一.作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. (1) 窃听风险(eavesdropping):第三方可以获 ...
- IDispose(), Finalize()
C# using 用法: 1. 作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型.(见例1-1) 2. 作为语句,用于定义一个范围,在此范围的末尾将释放对象.(见例1-2) (例子1- ...
随机推荐
- ES6生成器基础
ES6引进的最令人兴奋的特性就是一种新的函数生成方式,称为生成器(generator).名称有点奇怪,但是第一眼看上去行为更加奇怪.文章主要介绍生成器如何工作,然后让你明白为什么他们对于未来的JS会有 ...
- 金额input框控制只能小数点后有两位的有效数字
<%@include file="/WEB-INF/jsp/common/common.jsp" %> <title>价格录入限定</title> ...
- IRS-P6数据介绍
IRS-P6发射于2003年10月17日,星上携带三个传感器:多光谱传感器LISS4和LISS3,以及高级广角传感器AWIFS.LISS3传感器具有四个光谱波段分别位于可见光.近红外与短波红外区域,空 ...
- Oracle笔记 五、创建表、约束、视图、索引、序列、同义词、表空间
alter table userInfo add(msn varchar2(20)); 1.建表 create table userInfo ( id number(6), name varchar2 ...
- JS常用的设计模式(7)—— 外观模式
外观模式(门面模式),是一种相对简单而又无处不在的模式.外观模式提供一个高层接口,这个接口使得客户端或子系统更加方便调用.用一段再简单不过的代码来表示 var getName = function() ...
- Backup App's data without rooting the phone
First I'd like to let you know that my phone is Android 6.0 Marshmallow. So it works on the latest A ...
- Android IOS WebRTC 音视频开发总结(五十)-- 技术服务如何定价?
这篇文章最早是杜老师写的,看完感触很深,加上之前跟咨询公司的朋友也讨论过这方面的问题,所以结合自己的经验,做了些删改(得到了杜老师的授权). 先通过下面几个简单的问题来了解技术服务: 问题1:技术服务 ...
- Connect to a Windows PC from Ubuntu via Remote Desktop Connection
http://www.7tutorials.com/connecting-windows-remote-desktop-ubuntu A useful feature of Windows is be ...
- jQ $.extend用法
$.extend()函数 1.用将一个对象或多个对象的内容合并到目标对象, 2.如果多个对象具有相同的属性,则后者覆盖前者的属性值. 例子: var object1={ apple:1, banana ...
- WCF学习笔记(一)
WCF是什么? 官方解释: Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终结点发送至 ...