尝试从列表中接收数据时发生System.ObjectDisposedException [英] System.ObjectDisposedException when trying to receive the data from a List

查看:101
本文介绍了尝试从列表中接收数据时发生System.ObjectDisposedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在编写一个ASP.Net MVC应用程序,并且有一个专门用于数据库连接的特殊类.在我的HomeController中,我调用此特殊DB类的静态方法,该方法将所需的数据返回到对象中.我使用实体框架来实现这一目标.但是,当我尝试从控制器中使用列表时,出现了一个奇怪的异常.我相信问题在于,我有一个虚拟内部集合,在完成实体框架方法之后便将其丢弃.我可以访问主组字段,但不能访问内部列表.这是我的模型:

Hello I am writing an ASP.Net MVC application and I have a special class dedicated for Database connection. In my HomeController I call the static methods of this special DB class, which return the needed data into objects. 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. 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; }
}

当我尝试获取CourseTemplate结果时,出现此异常:

When I try to get the CourseTemplate results I get this exception:

异常详细信息:System.ObjectDisposedException:ObjectContext实例已被处置,不能再用于需要连接的操作.

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

我试图单独调查它,但是我发现的所有建议都是返回一个List而不是一个查询.这也是我的DBConnection方法的代码:

I tried to investigate it alone, but everything I found is the suggestion to return a List instead of a query. Here is also the code for my DBConnection method:

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

            return query.ToList();
        }
    }

我在做什么错了?

推荐答案

您的原始查询仅加载Teacher对象.集合被设置为延迟加载,直到您访问它们才被获取.不幸的是,此时,您的objectcontext已关闭,因此您无法再从数据库中获取任何内容.

Your original query only loads the Teacher objects. 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.

最好的解决办法是将查询更新为紧急加载您要使用的其他数据:

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;

您还必须添加using System.Data.Entity来访问采用lambda的Include()方法.请勿使用带有字符串的字符串.

You also have to add a using System.Data.Entity to get access to the Include() method that takes a lambda. Don't use the one taking strings.

这篇关于尝试从列表中接收数据时发生System.ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆