加载相关实体多个分支和多个级别
我在写一个应用程序,我需要一个特定的学生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' 抛出
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
也
:
我已经试过型 'System.ObjectDisposedException' 的例外
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
编辑:评价还没有装入evaluationround
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
部分会照顾到这一点。
回答:
我怀疑是因为延迟加载导致发生错误。 EvaluationRound
或EvaluationRoundProject
实体的导航属性为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
导航性能,他们在哪里使用后方面已经布置。
以上是 加载相关实体多个分支和多个级别 的全部内容, 来源链接: utcz.com/qa/266264.html