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

后端 未结 10 2167
清酒与你
清酒与你 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.

    0 讨论(0)
  • 2020-12-01 23:51

    I found the best way to handle this and keep the using statement you just need to use the include, see sample below:

    using (var ctx = new Context(this.connectionString)) {
      var query = ctx.[maintable]
        .Include(x => x.[theothertable])
        .FirstOrDefaultAsync(u => u.UserName.Equals(userName));
    }
    
    0 讨论(0)
  • 2020-12-01 23:58

    It sounds like some lazy-loading or delayed evaluation that's happening; you can't assume objects are "loaded" until you actually attempt to read from them.

    You need to maintain your DataContext until you are completely done handling the objects retrieved from the database to avoid these errors.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-01 23:59

    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.

    0 讨论(0)
  • 2020-12-02 00:01
    using (EmployeeContext db= new EmployeeContext())
        {
            var lst = db.Employees.Select(p=> new {
                EmployeeID = p.EmployeeID,
                Name = p.Name,
                Salary = p.Salary,
                Position = p.Position,
                Age = p.Age,
                Office = p.Office
            }).ToList();
            return Json(lst, JsonRequestBehavior.AllowGet);
        }
    
    0 讨论(0)
提交回复
热议问题