簡體   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