简体   繁体   English

尝试从List接收数据时System.ObjectDisposedException

[英]System.ObjectDisposedException when trying to receive the data from a List

Hello I am writing an ASP.Net MVC application and I have a special class dedicated for Database connection. 您好我正在编写ASP.Net MVC应用程序,我有一个专门用于数据库连接的特殊类。 In my HomeController I call the static methods of this special DB class, which return the needed data into objects. 在我的HomeController中,我调用这个特殊DB类的静态方法,它将所需的数据返回到对象中。 I use the Entity Framework in order to achieve this. 我使用实体框架来实现这一目标。 However I get a strange exception when I try to use the List from my Controller. 但是当我尝试使用Controller中的List时,我遇到了一个奇怪的异常。 I believe that the problem is that I have a virtual inner collection that is disposed after the entity framework methods are done. 我认为问题在于我有一个虚拟内部集合,在实体框架方法完成后处理。 I can access the main-group fields, but can't access the inner Lists. 我可以访问主组字段,但无法访问内部列表。 Here is my model: 这是我的模型:

public partial class Teacher
{
    public Teacher()
    {
        this.TeacherRelationships = new List<TeacherRelationship>();
        this.CourseTemplates = new List<CourseTemplate>();
        this.Orders = new List<Order>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string DescriptionText { get; set; }
    public Nullable<int> Phone { get; set; }
    public string Email { get; set; }
    public string PictureFilePath { get; set; }
    public virtual ICollection<TeacherRelationship> TeacherRelationships { get; set; }
    public virtual ICollection<CourseTemplate> CourseTemplates { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

When I try to get the CourseTemplate results I get this exception: 当我尝试获取CourseTemplate结果时,我得到以下异常:

Exception Details: System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. 异常详细信息:System.ObjectDisposedException:ObjectContext实例已被释放,不能再用于需要连接的操作。

I tried to investigate it alone, but everything I found is the suggestion to return a List instead of a query. 我试图单独调查它,但我找到的所有内容都是建议返回List而不是查询。 Here is also the code for my DBConnection method: 这也是我的DBConnection方法的代码:

    public static List<Teacher> returnAllTeachers()
    {
        using (var db = new Context())
        {
            var query = from th in db.Teachers
                        select th;

            return query.ToList();
        }
    }

What am I doing wrong? 我究竟做错了什么?

Your original query only loads the Teacher objects. 您的原始查询仅加载Teacher对象。 The collections are set to be lazy loading to not be fetched until you access them. 集合设置为延迟加载,直到您访问它们为止。 Unfortunately at that point, your objectcontext is closed so you can no longer fetch anything from your database. 不幸的是,您的objectcontext已关闭,因此您无法再从数据库中获取任何内容。

The best fix is to update the query to eager load the additional data you want to use: 最好的解决方法是更新查询以急切加载您要使用的其他数据:

var query = from th in db.Teachers.Include(t => t.Orders)
            select th;

You also have to add a using System.Data.Entity to get access to the Include() method that takes a lambda. 您还必须添加一个using System.Data.Entity来访问带有lambda的Include()方法。 Don't use the one taking strings. 不要使用带弦的那个。

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

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