2017-12-27 451 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

相关问题