在當前的MVC4.0項目中,我使用的是實體框架4.1數據庫優先模型。這種結構的實體框架4.1渴望加載完成對象
部分包括以下表
compGroupData SurveyData SecondaryData
compGroupData和SurveyData未在數據庫
SecondaryData經由接合到SurveyData上的一對一的關係連接一個外鍵SurveyData.surveydatakey = SecondaryData.surveydatakey
在我的項目中,我有一個類Compa risonWithData定義爲:
public class ComparisonWithData
{
public compGroupData compgrp { get; set; }
public SurveyData surveydata { get; set; }
public ComparisonWithData()
{
compgrp = new compGroupData();
surveydata = new SurveyData();
}
}
這給了我一個特定比較組的結果集以及與此匹配的數據。
在我已經通過下面的查詢檢索到該數據過去:
List<ComparisonWithData> comparisonwithdata = ((from compgrp in db.compGroupDatas
where compgrp.grpYear == rptyear && compgrp.CompGroupID == ccompgrp.CompGrpID
join surveydata in db.SurveyDatas on new { compgrp.companyid, SurveyYear = (Int32)compgrp.SurveyYear } equals new { companyid = surveydata.companyid, SurveyYear = surveydata.surveyyear }
select new ComparisonWithData
{
compgrp = compgrp,
surveydata = surveydata,
}
)).ToList();
隨着數據最近的變化現在我還需要參考SecondaryData但由於記錄的數量確實需要這樣加載Eagerly而不是Lazy。 (循環期間的延遲加載會導致數千次數據庫調用)
我已經看過在surveydata上使用「Include」方法以及將初始查詢作爲ObjectQuery進行投射並執行包含關閉操作。
第一種方法沒有加載,第二種方法似乎總是返回空對象。
是否有一種方法來Eager加載SurveyData的SecondaryData,或者我應該一起看看不同的方法。
我對這個唯一的限制是我不能上去EF5因爲我們對.NET 4.5
任何援助將不勝感激的限制。
謝謝。
我發現另一篇文章非常相似,這其中一個這很好。唯一的區別是我沒有結束這兩個步驟。當在Surveydata.SecondaryData中加載「secondarydata」對象時也會直接加載。我希望有另一種更直接的方式來做,但這種方法起作用,這是最好的答案。再次感謝您的幫助 –