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

后端 未结 10 2226
清酒与你
清酒与你 2020-12-01 23:21

I am using EF 4 to retrieve a list of Employees.

public ContentResult AutoCompleteResult(string searchText)
{
    List list = Employee.GetAll         


        
10条回答
  •  爱一瞬间的悲伤
    2020-12-01 23:58

    I had a variation on this problem. The data context was not closed, but the Object context instance has been disposed error was getting thrown anyway.

    It turned out that the object had a self referential foreign key (ie the foreign key referred back into the same table). Accessing the navigation property in when it refers to a null, you get the objectcontext disposed exception instead of, say, a null pointer exception.

    eg:

    var a = myObject.Name; // works because myObject still has open object context
    var b = myObject.SelfReference; // throws objectcontext disposed if SelfReference is null
    

提交回复
热议问题