使用实体框架设计了数据访问层,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));
});
这里如何消除多个数据库调用?