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

时间:2017-12-27 16:18:09

标签: c# entity-framework linq

我正在编写一个应用程序,我需要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)(new 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部分会照顾这个。

1 个答案:

答案 0 :(得分:0)

我怀疑由于延迟加载而发生错误。 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导航属性以及在已经处理的上下文后它们在哪里使用。