简体   繁体   English

加载相关实体的多个分支和多个级别

[英]Loading related entities multiple branches and multiple levels

I'm writing an application where I need the EvaluationRounds with a particular student. 我正在编写一个需要与特定学生一起使用EvaluationRounds的应用程序。

Everything starts with the project. 一切都始于项目。 A project has many groups. 一个项目有许多小组。 A group has many members, but one member can also be in many groups. 一个小组有许多成员,但一个成员也可以属于许多小组。 This is done by the associative table ProjectGroupMembers On the other hand, a project has many evaluation rounds. 这是通过关联表ProjectGroupMembers完成的。另一方面,一个项目具有许多评估回合。

Currently I have this linq statement: 目前,我有此linq语句:

from r in _context.EvaluationRounds.Include(e => e.EvaluationRoundProject.ProjectGroups.Select(pg => pg.Persons))
                                   .Include(e => e.Evaluations)
     join g in _context.ProjectGroups on r.ProjectId equals g.ProjectId
     join m in _context.ProjectGroupMembers on g.ProjectGroupId equals m.GroupId
     where m.PersonId == studentId && r.EvaluationRoundStartTime < DateTime.Now && r.EvaluationRoundEndTime > DateTime.Now
     select r

We use the using statement to dispose of the dbcontext as soon as we have the list. 有了列表,我们就立即使用using语句处理dbcontext。

The problem is that the EvaluationRoundProject and its relatives are not loaded with the EvaluationRounds . 问题在于EvaluationRoundProject及其亲属未加载EvaluationRounds This is what we get: 这是我们得到的:

'((System.Data.Entity.DynamicProxies.EvaluationRound_7400F2ED13550F1E92655A802808E4B94D454A30979C80D0EEED31D0CB7D7005)(new System.Collections.Generic.Mscorlib_CollectionDebugView(activeEvaluationrounds).Items[0])).EvaluationRoundProject' threw an exception of type 'System.ObjectDisposedException' '((System.Data.Entity.DynamicProxies.EvaluationRound_7400F2ED13550F1E92655A802808E4B94D454A30979C80D0EEED31D0CB7D7005)(新System.Collections.Generic.Mscorlib_CollectionDebugView(activeEvaluationrounds).Items [0]))。EvaluationRoundProject' 扔类型 'System.ObjectDisposedException' 的一个异常

I have tried: 我努力了:

from r in _context.EvaluationRounds.Include("EvaluationRoundProject").Include(e => e.EvaluationRoundProject.ProjectGroups.Select(pg => pg.Persons)).Include(e => e.Evaluations)
     join g in _context.ProjectGroups on r.ProjectId equals g.ProjectId
     join m in _context.ProjectGroupMembers on g.ProjectGroupId equals m.GroupId
     where m.PersonId == studentId && r.EvaluationRoundStartTime < DateTime.Now && r.EvaluationRoundEndTime > DateTime.Now
     select r

and also 并且

from r in _context.EvaluationRounds.Include(a => a.EvaluationRoundProject).Include(e => e.EvaluationRoundProject.ProjectGroups.Select(pg => pg.Persons)).Include(e => e.Evaluations)
     join g in _context.ProjectGroups on r.ProjectId equals g.ProjectId
     join m in _context.ProjectGroupMembers on g.ProjectGroupId equals m.GroupId
     where m.PersonId == studentId && r.EvaluationRoundStartTime < DateTime.Now && r.EvaluationRoundEndTime > DateTime.Now
     select r

Edit : The evaluations also do not load into the evaluationround 编辑 :评估也不会加载到评估轮中

Edit2 : this is the whole using code Edit2 :这是整个使用代码

using (_context = new PeerEvaluationContext())
{
    var activeEvaluationrounds = from r in _context.EvaluationRounds.Include(e => e.EvaluationRoundProject.ProjectGroups.Select(pg => pg.Persons)).Include(e => e.Evaluations)
                                         join g in _context.ProjectGroups on r.ProjectId equals g.ProjectId
                                         join m in _context.ProjectGroupMembers on g.ProjectGroupId equals m.GroupId
                                         where m.PersonId == studentId && r.EvaluationRoundStartTime < DateTime.Now && r.EvaluationRoundEndTime > DateTime.Now
                                         select r;
    return activeEvaluationrounds.ToList();
}

Edit 3 : this problem occurs because lazy loading is used. 编辑3 :发生此问题,因为使用了延迟加载。 But I went looking on the internet and they said the include part would take care of this. 但是我继续在互联网上寻找,他们说include部分会解决这个问题。

I suspected that the error occured because of lazy loading. 我怀疑该错误是由于延迟加载而发生的。 EvaluationRound or EvaluationRoundProject entities have navigation properties which is virtual and EF is attempting to load data in somewhere after _context already disposed. EvaluationRoundEvaluationRoundProject实体具有virtual导航属性,并且EF尝试在已放置_context之后的某个位置加载数据。 So, would you try to use another class to select query; 因此,您会尝试使用另一个类来选择查询吗?

var activeEvaluationrounds = from r in _context.EvaluationRounds.Include(e => e.EvaluationRoundProject.ProjectGroups.Select(pg => pg.Persons)).Include(e => e.Evaluations)
    join g in _context.ProjectGroups on r.ProjectId equals g.ProjectId
    join m in _context.ProjectGroupMembers on g.ProjectGroupId equals m.GroupId
    where m.PersonId == studentId && r.EvaluationRoundStartTime < DateTime.Now && r.EvaluationRoundEndTime > DateTime.Now
    select new EvaluationRoundDto
    {
        EvaluationRoundId = r.EvaluationRoundId,
        ProjectId = r.ProjectId
        //etc.
    };

I think you should check virtual navigation properties and where are they used after context already disposed. 我认为您应该检查virtual导航属性,并在上下文已经处理之后在哪里使用它们。

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

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