2017-12-27 379 views
0

我在寫一個應用程序,我需要一個特定的學生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部分會照顧到這一點。

+0

use .ToList()? –

+0

已經這樣做了,將這些信息添加到問題 – freshrebel

回答

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導航性能,他們在哪裏使用後方面已經佈置。

+0

感謝您的答案。實際上使用延遲加載是因爲在大多數情況下,我們需要評估而不是其項目。我會嘗試這個解決方案,但無論我看,像這裏[鏈接](https://msdn.microsoft.com/en-us/library/jj574232(v = vs.113).aspx),他們說'包括部分'會將項目加載到評估中,以便您可以訪問它們。所以我認爲這就夠了,爲什麼不呢? – freshrebel

+0

我試過這個,我得到錯誤'System.NotSupportedException:'實體或複雜類型'AP.PeerEvaluations.DAL.Contexts.EvaluationRound'不能在LINQ to Entities查詢中構造。' – freshrebel

+0

你應該創建一個新類來選擇查詢,比如「select new EvaluationRoundDto」。不使用實體類。 – lucky