简体   繁体   English

尝试列出对象时出现System.ObjectDisposedException

[英]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 得到了System.ObjectDisposedException并且Evenements为null

Using many to many relation between Personne and Evenement so cannot read the join table in the db. 在Personne和Evenement之间使用多对多关系,因此无法读取db中的联接表。

The problem is that the Evenements are probably being loaded using lazy loading (ie not retrieved until the property is accessed). 问题在于, Evenements可能正在使用延迟加载进行加载(即,直到访问该属性才可以检索到)。

When you are coding web applications the connection used to fetch information is usually closed when the HTTP request ends. 对Web应用程序进行编码时,通常在HTTP请求结束时关闭用于获取信息的连接。

Thus when you on the second request try to access Evenements property the connection have been closed. 因此,当您在第二个请求上尝试访问Evenements属性时,连接已关闭。 And you therefore get the ObjectDisposedException . 因此,您将获得ObjectDisposedException

You can activate eager loading by using the Include method: http://www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx 您可以使用Include方法激活预加载: http : //www.entityframeworktutorial.net/EntityFramework4.3/eager-loading-with-dbcontext.aspx

That way the property have been loaded and stored for all future requests. 这样,就可以为将来的所有请求加载和存储属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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