繁体   English   中英

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

[英]Loading related entities multiple branches and multiple levels

我正在编写一个需要与特定学生一起使用EvaluationRounds的应用程序。

一切都始于项目。 一个项目有许多小组。 一个小组有许多成员,但一个成员也可以属于许多小组。 这是通过关联表ProjectGroupMembers完成的。另一方面,一个项目具有许多评估回合。

目前,我有此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

有了列表,我们就立即使用using语句处理dbcontext。

问题在于EvaluationRoundProject及其亲属未加载EvaluationRounds 这是我们得到的:

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

我努力了:

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

并且

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

编辑 :评估也不会加载到评估轮中

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();
}

编辑3 :发生此问题,因为使用了延迟加载。 但是我继续在互联网上寻找,他们说include部分会解决这个问题。

我怀疑该错误是由于延迟加载而发生的。 EvaluationRoundEvaluationRoundProject实体具有virtual导航属性,并且EF尝试在已放置_context之后的某个位置加载数据。 因此,您会尝试使用另一个类来选择查询吗?

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.
    };

我认为您应该检查virtual导航属性,并在上下文已经处理之后在哪里使用它们。

暂无
暂无

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

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