我有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;
}
我现在已将其添加到列表中,现在我收到错误消息“消息”:“序列化类型为\ u0027IncidentTracker.Incident \ u0027的对象时检测到循环引用。 – user516883
@ user516883它看起来像'RootCause'也有一个参考事件。另外,请确保你没有'LoadsWith(irc => irc.Incident)'你可以发布你的表DDL的3桌吗? –
StuartLC
序列化异常与loadoptions(也就是说“Cycles not allowed”)没有关系,而是与之后发生的json/xml序列化(我认为)有关。 –