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

后端 未结 10 2205
清酒与你
清酒与你 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:51

    You could turn off lazy loading to resolve this problem.
    Inside your 'using' block, try this:

    yourObjectContext.ContextOptions.LazyLoadingEnabled = false;
    

    After doing this, I was able to serialize my EF (DbContext-generated) POCO to JSON without any issue.

    *Note: Since I've turned off lazy loading... I explicitly pull in related objects I need ahead of time (mostly with .Include() in my query) before the object is serialized to JSON.

提交回复
热议问题