简体   繁体   中英

System.ObjectDisposedException when trying to list an object

Trying to fix this error

public ActionResult MyEvents()
{
    Personne personne = (Personne)Session["User"];
        personne.Evenements.ToList();
        return View(personne);

}

Got the System.ObjectDisposedException and Evenements is null

Using many to many relation between Personne and Evenement so cannot read the join table in the db.

The problem is that the Evenements are probably being loaded using lazy loading (ie not retrieved until the property is accessed).

When you are coding web applications the connection used to fetch information is usually closed when the HTTP request ends.

Thus when you on the second request try to access Evenements property the connection have been closed. And you therefore get the ObjectDisposedException .

You can activate eager loading by using the Include method: http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx

That way the property have been loaded and stored for all future requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM