2012-09-10 44 views
1

我有3个表格。事件,Rootcauses和IncidentRootCauses。表incidentrootcauses与事件作为一对多关联。当我得到的事件对象,没有为我所料根本原因对象的列表,而不是我得到(在我立即窗口)LINQ to sql加载儿童一对多关系

var rt = incident.IncidentRootCauses; 

{} System.Data.Linq.EntitySet计数 :1

HasLoadedOrAssignedValues: true 

IsDeferred: false 

我希望能够在查询运行时加载所有的rootcause对象。我知道问题是事件并没有明确地与rootcause表相关联。感谢您的帮助。

using (var db = new IncidentTrackerDataContext()) 
      { 
       var lo = new DataLoadOptions(); 
       lo.LoadWith<Incident>(incidents => incidents.ReportedTo); 
       lo.LoadWith<Incident>(incidents => incidents.Shift); 
       lo.LoadWith<Incident>(incidents => incidents.Machine); 
       lo.LoadWith<Incident>(incidents => incidents.Department); 
       lo.LoadWith<Incident>(incidents => incidents.IncidentRootCauses); 
       lo.LoadWith<IncidentRootCause>(i => i.RootCause); 

       // lo.LoadWith<Incident>(incidents => incidents.IncidentManagers); 
       // lo.LoadWith<Incident>(incidents => incidents.IncidentMembers); 
       // lo.LoadWith<Incident>(incidents => incidents.IncidentWitnesses); 
       db.LoadOptions = lo; 
       db.DeferredLoadingEnabled = false; 
       Incident incident = (from i in db.Incidents 
            where i.IncidentReportID == new Guid(incidentID) 
            select i).FirstOrDefault(); 

       return incident; 
      } 

回答

1

这听起来像你也想要第二级渴望加载的实体。

同LoadOptions,请尝试:

lo.LoadWith<IncidentRootCause>(irc => irc.RootCause); 
+0

我现在已将其添加到列表中,现在我收到错误消息“消息”:“序列化类型为\ u0027IncidentTracker.Incident \ u0027的对象时检测到循环引用。 – user516883

+0

@ user516883它看起来像'RootCause'也有一个参考事件。另外,请确保你没有'LoadsWith (irc => irc.Incident)'你可以发布你的表DDL的3桌吗? – StuartLC

+0

序列化异常与loadoptions(也就是说“Cycles not allowed”)没有关系,而是与之后发生的json/xml序列化(我认为)有关。 –

0

您可能要检查生成的SQL。当试图加载多个孩子时,一些加载选项可能会被忽略,因为这可能意味着拉太多的记录。因此,在使用条款已经退出之后,您的一些子记录可能不会被提取。