我在寫一個應用程序,我需要一個特定的學生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
部分會照顧到這一點。
use .ToList()? –
已經這樣做了,將這些信息添加到問題 – freshrebel