System.ObjectDisposedException:本ObjectContext的实例已设置,不能再被用于需要连接的操作 [英] System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
问题描述
我使用EF 4检索员工列表。
公共ContentResult类型AutoCompleteResult(字符串SEARCHTEXT)
{
清单<员工>清单= Employee.GetAllCurrentEmployees();
清单<员工> filteredEmployees =名单
。凡(X =方式>。x.GetName()ToLower将()包含(searchText.ToLower()))
.ToList(); 的JavaScriptSerializer jsonSerializer =新的JavaScriptSerializer();
变种jsonString = jsonSerializer.Serialize(filteredEmployees)的ToString();
返回内容(jsonString);
}
该列表检索确定,但是当我序列化,我得到这个例外;
System.ObjectDisposedException:ObjectContext中实例已
布置,并且可以不再被用于
需要连接的操作。
生成:星期三,2010年11月17日16时06分56秒GMTSystem.ObjectDisposedException:ObjectContext中实例已
布置,并且可以不再被用于
需要连接的操作。
在
System.Data.Objects.ObjectContext.EnsureConnection()
在
System.Data.Objects.ObjectQuery1.GetResults(可空
1
forMergeOption)在
System.Data.Objects.ObjectQuery1.Execute(MergeOption
1.Load(名单
mergeOption)在
System.Data.Objects.DataClasses.EntityCollection1
1.Load(MergeOption
收集,MergeOption mergeOption)结果
在
System.Data.Objects.DataClasses.EntityCollection
mergeOption)在
System.Data.Objects.DataClasses.RelatedEnd.Load()
在
System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
在
System.Data.Objects.DataClasses.EntityCollection<$c$c>1.System.Collections.IEnumerable.GetEnumerator()
在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable的
枚举,StringBuilder的某人,的Int32
深度,Hashtable的objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable的
枚举,StringBuilder的某人,的Int32
深度,Hashtable的objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(对象
0,StringBuilder的某人,深度Int,
哈希表objectsInUse,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(对象
OBJ,StringBuilder的输出,
SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(对象
OBJ,SerializationFormat
serializationFormat)在
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(对象
OBJ)在
SHP.Controllers.EmployeeController.AutoCompleteResult(字符串
SEARCHTEXT)在C:\\ Documents和
设置\\ geoffreypayne \\我
文档\\ Visual Studio中
2010 \\项目\\ MVC \\ SHP \\ SHP \\ \\控制器EmployeeController.cs:行
623在lambda_method(封闭,
ControllerBase,对象[])在
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
控制器,对象[]参数)结果
在
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext,IDictionary的 2
参数)在
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
controllerContext,ActionDescriptor
actionDescriptor,IDictionary的2
1续)结果
参数)在
System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
在
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
过滤器,ActionExecutingContext
preContext,Func键
在
System.Web.Mvc.ControllerActionInvoker&LT;> C_的 DisplayClassd&LT;> C 的_DisplayClassf.b__c()
在
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext,IList的1的过滤器,
2参数)在
ActionDescriptor actionDescriptor,
IDictionary的
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext,字符串actionName)
块引用>我觉得这很奇怪。我已经获取员工的名单和DataContext的已被释放。所以,我为什么会得到这个错误?
解决方案这听起来像你有没有尚未加载一些延迟加载的关系属性(其中有一个相关的N + 1的性能问题)。您可以尝试立即加载来看看这可以帮助;否则,明确装载数据列表中的每个项目,的前的关闭对象的上下文。
I am using EF 4 to retrieve a list of Employees.
public ContentResult AutoCompleteResult(string searchText) { List<Employee> list = Employee.GetAllCurrentEmployees(); List<Employee> filteredEmployees = list .Where(x => x.GetName().ToLower().Contains(searchText.ToLower())) .ToList(); JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); var jsonString = jsonSerializer.Serialize(filteredEmployees).ToString(); return Content(jsonString); }
The list is retrieved OK, but when I serialize it, I get this exception;
System.ObjectDisposedException: The ObjectContext instance has been
disposed and can no longer be used for operations that require a connection. Generated: Wed, 17 Nov 2010 16:06:56 GMT
System.ObjectDisposedException: The ObjectContext instance has been
disposed and can no longer be used for operations that require a connection. at System.Data.Objects.ObjectContext.EnsureConnection() at System.Data.Objects.ObjectQuery
1.GetResults(Nullable
1 forMergeOption) at System.Data.Objects.ObjectQuery1.Execute(MergeOption mergeOption) at System.Data.Objects.DataClasses.EntityCollection
1.Load(List1 collection, MergeOption mergeOption)
1.Load(MergeOption mergeOption) at System.Data.Objects.DataClasses.RelatedEnd.Load() at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() at System.Data.Objects.DataClasses.EntityCollection
at System.Data.Objects.DataClasses.EntityCollection1.System.Collections.IEnumerable.GetEnumerator() at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj) at SHP.Controllers.EmployeeController.AutoCompleteResult(String searchText) in C:\Documents and Settings\geoffreypayne\My Documents\Visual Studio 2010\Projects\MVC\SHP\SHP\Controllers\EmployeeController.cs:line 623 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c_DisplayClassd.<>c_DisplayClassf.b__c() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary
2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)I find this very odd. I have already retrieved the list of employees and the DataContext has been disposed. So why would I get this error?
解决方案It sounds like you have some lazily loaded relationship properties that have not yet loaded (which has an associated "n+1" performance concern). You can try eager loading to see if this helps; otherwise, explicitly load the data for each item in the list, before you close the object-context.
这篇关于System.ObjectDisposedException:本ObjectContext的实例已设置,不能再被用于需要连接的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!