2013-08-29 157 views
3

我有兩個型號,一個是設備類型,另一個是設備的問題得到解決外鍵值。這裏的關係是一個(DeviceType)到很多(DeviceIssues)。下面是型號:使用實體框架代碼首先

public class DeviceType : ModelBase 
{ 
    public string Manufacture { get; set; } 
    public DeviceTypes Type { get; set; } 
    public string Model { get; set; } 

    public virtual ICollection<DeviceIssue> Issues { get; set; } 
} 

public class DeviceIssue : ModelBase 
{ 
    public string Description { get; set; } 
    public decimal RepairCost { get; set; } 

    public int DeviceTypeId { get; set; } 
    public virtual DeviceType DeviceType { get; set; } 
} 

public abstract class ModelBase 
{ 
    public int Id { get; set; } 
    public Guid Guid { get; set; } 
    public DateTime FirstCreated { get; set; } 
    public string LastUpdateUser { get; set; } 
    public DateTime LastUpdateDt { get; set; } 
    public bool IsDeleted { get; set; } 
} 

我必須填入幾個實體數據庫,使用種子的方法,以及它們的外鍵都完好無損。但是,當我得到設備類型的列表時,我也沒有得到每個設備的相關問題列表。我使用的是AutoMapper,但是,調試存儲查詢結果的變量也不會顯示數據。下面是我使用,使調用的代碼:

var result = new List<DeviceTypeDataContract>(); 
using (var context = new DSPEntities()) 
{ 
    var devices = context.DeviceTypes; 
    foreach (var device in devices) 
    { 
     //var issues = context.DeviceIssues.Where(i => i.DeviceTypeId == device.Id).ToList(); 

     var devi = Mapper.Map<DeviceType, DeviceTypeDataContract>(device); 
     result.Add(devi); 
    } 
} 

現在的規定,在調試時,檢查變量deviceforeach(var device in devices),顯示如下類似的結果每次:

device.Issues = null, 
device.manufacture = "Apple", 
device.Model = "iPhone 3S", 
device.Type = DeviceTypes.SmartPhone, 

如果您請注意我的代碼中有一段註釋掉的代碼。這是因爲評論它引發以下內部異常:

{"There is already an open DataReader associated with this Command which must be closed first."}

我不知道這與我的具體問題,或者不這樣做,雖然。

我的問題是,如何取回從這些關係中的數據?

注:關係確實存在,因爲外面的foreach語句,調用下面:

var issues = context.DeviceIssues.Where(i => i.DeviceTypeId == 1).ToList(); 

給出以下結果:

issues[6].Description = "Water Damage", 
issues[6].RepairCost = 0.00, 
issues[6].DeviceTypeId = 1, 
issues[6].DeviceType = [+]{Data.Model.DeviceType}, 
// The last property holds values similar to above. 
// Except now the list of issues is populated. 

注意上面的評論:「除了現在的問題列表已填充。「

+0

['virtual'被推斷延遲加載(http://stackoverflow.com/問題/ 5597760 /什麼效果燦的虛擬關鍵字具備的,在實體框架-4-1-POCO碼-FI)。您可以在'DbContext'調整延遲加載或使用'.INCLUDE()'方法,當你做查詢主對象(所以它抓住協會以及)。例如'context.DeviceTypes.Include(x => x。DeviceIssues)' –

回答

5

當你裝飾與virtual協會你推斷他們是lazy loaded。這意味着,主要採集(在這種情況下DeviceTypes)被加載但,直到您嘗試訪問它,Issues將不會與任何信息填充。

有多種方式來解決這個問題,但最簡單的(也是最有效的)是當你需要使用Include方法將其加載的關聯。例如

var devices = context.DeviceTypes.Include(x => x.Issues); 

這將加載原始集合(DeviceTypes)以及填充Issues每種類型。這通常是因爲你只抓取您需要的信息在需要時獲得工作完成你的單位的最佳途徑。

如果你發現這是常見的,但是,你總能在你DbContext構造調整延遲加載:

public DSPEntities() 
{ 
    this.Configuration.LazyLoadingEnabled = false; 
} 
+0

使用此方法會產生以下錯誤:'無法解析符號'Include''是否存在使用此方法所需的程序集? – Oxymoron

+1

yes:System.Data.Entity; – Oxymoron

+0

我在一個WCF項目中使用它,並且在調用使用此代碼的特定操作合同時,會發生以下錯誤:「接收到http://xx.xx.x.xx的HTTP響應時發生錯誤:8200 /服務/WCFClient.svc。這可能是由於服務端點綁定不使用HTTP協議。這也可能是由於HTTP請求上下文被服務器中止(可能是由於服務關閉)。查看服務器日誌以獲取更多詳細信息。]' – Oxymoron

相關問題