0

使用实体框架设计了数据访问层,PackageInstance 对象的示例 POCO 结构是

public class PackageInstance
{
  public virtual long PackageInstanceId {set;get;}
  public virtual boolean IsDeleted {set;get;}
  public virtual List<Session> Sessions {set;get;}
}

public class Session
{
  public virtual long SessionId {set;get;}
  public virtual long PackageInstanceId {set;get;}
  public virtual boolean IsDeleted {set;get;}
  public virtual List<Note> Notes {set;get;}
}

public class Note
{
  public virtual long NoteId {set;get;}
  public virtual long SessionId {set;get;}
  public virtual boolean IsDeleted {set;get;}
  public virtual List<Documents> Document {set;get;}
}

我需要在单个方法调用中加载 PackageInstance 对象及其子对象,而不是单独加载每个对象。

  var packageInstanceDB = entity.PackageInstances.First(p => p.PurchaseSessionId == purhcaseSessionId);                   
  //There is a DB call happening here to load the Session.
  packageInstanceDB.Sessions.Where(s=>!s.IsActive).ForEach(s => 
  {
       //Again there is a DB call happening here to load the associated session notes.
       s.Notes.Where(sn => !sn.IsDeleted).ToList().ForEach(sd=>
       //Again there is a DB call happening here to load the associated note documents.
       sd.Documents.Where(doc=>!doc.IsDeleted));
  });

这里如何消除多个数据库调用?

4

1 回答 1

1

免责声明:我是Entity Framework Plus项目的所有者

EF+ Query IncludeFilter 功能允许过滤相关实体。支持EF5

var packageInstanceDB = entity.PackageInstances
    .IncludeFilter(x => x.Sessions)
    .IncludeFilter(x => x.Sessions.Select(y => y.Notes.Where(sn => !sn.IsDeleted)))
    .IncludeFilter(x => x.Sessions.SelectMany(y => y.Notes.Where(sn => !sn.IsDeleted)).Select(z => z.Documents.Where(sn => !sn.IsDeleted)))
    .First(p => p.PackageInstanceId == purhcaseSessionId);

注意:必须包含每个路径

Wiki:EF+ 查询包含过滤器

于 2017-03-16T14:00:32.487 回答