2013-07-08 46 views
0

當試圖加載PriceGridRow時,將填充PriceGridColumn的索引和值屬性,但Id和ProduceGridRowId不是。如果我試圖明確包含PriceGridColumns,它會得到重複的列(即我有10列,但由EF返回的對象有20),並且返回的列的一半已完全填充,另一半不是。當使用實體框架進行Eager加載時缺少信息

我一直在拉我剩餘的頭髮,試圖弄清楚爲什麼會發生這種情況。任何人都可以看到基於我的配置爲什麼它會採取這種方式?謝謝!

我用它來獲得列的代碼是:

public override PriceGrid GetLoadedById(object id) 
{ 
    var priceGrid = Entities 
     Include(x => x.PriceGridRows.Select(o => o.PriceGridColumns)) 
     .FirstOrDefault(x => x.Id == (int) id); 


    return priceGrid; 
} 

這裏是有問題的類

public class PriceGrid : DomainEntity<int> 
{ 
    public string Description { get; set; } 
    public Product Product { get; set; } 
    public int ProductId { get; set; } 

    public List<PriceGridRow> PriceGridRows 
    { 
     get { return _priceGridRow; } 
     set { _priceGridRow = value; } 
    } 
} 
public class PriceGridRow : DomainEntity<int> 
{ 
    public PriceGrid PriceGrid { get; set; } 
    public int PriceGridId { get; set; } 

    public ProductOption ProductOption { get; set; } 
    public int ProductOptionId { get; set; } 
    public List<PriceGridColumn> PriceGridColumns { get; set; } 

} 

最後嵌套的第三級

public class PriceGridColumn : DomainEntity<int> 
{ 
    public PriceGridRow PriceGridRow { get; set; } 
    public int PriceGridRowId { get; set; } 

    public int Index { get; set; } 
    public decimal Value { get; set; } 
} 

這裏是我的映射文件

public class PriceGridMap : EntityTypeConfiguration<PriceGrid> 
{ 
    public PriceGridMap() 
    { 
     HasKey(x => x.Id); 

     Property(x => x.Description); 
     HasRequired(x => x.Product); 

     HasMany(x => x.PriceGridRows) 
      .WithRequired(x => x.PriceGrid) 
      .HasForeignKey(x => x.PriceGridId) 
      .WillCascadeOnDelete(); 
    } 
} 
public class PriceGridRowMap : EntityTypeConfiguration<PriceGridRow> 
{ 
    public PriceGridRowMap() 
    { 
     HasKey(x => x.Id); 
     HasRequired(x => x.ProductOption); 
     HasMany(x => x.PriceGridColumns) 
      .WithRequired(x => x.PriceGridRow) 
      .HasForeignKey(x => x.PriceGridRowId) 
     .WillCascadeOnDelete(); 
    } 
} 
public class PriceGridColumnMap : EntityTypeConfiguration<PriceGridColumn> 
{ 
    public PriceGridColumnMap() 
    { 
     HasKey(x => x.Id); 
     Property(x => x.Index); 
     Property(x => x.Value); 
     HasRequired(x => x.PriceGridRow); 
    } 
} 
+0

下次更仔細地選擇問題標籤。我現在編輯了他們。它們對於提高獲得答案的機會非常重要。 – Slauma

回答

0

嘗試從PriceGridColumnMap刪除此映射線:

HasRequired(x => x.PriceGridRow); 

這基本上意味着關係的PriceGridRow導航屬性屬於不具有逆導航屬性。這是一條捷徑:

HasRequired(x => x.PriceGridRow) 
    .WithMany()... 

但是,這是在與映射矛盾PriceGridRowMap

HasMany(x => x.PriceGridColumns) 
    .WithRequired(x => x.PriceGridRow)... 

其說,PriceGridRow導航屬性確實有逆導航屬性,即PriceGridColumns

+0

謝謝,我會給它一個去,並報告回來。 – Kelly

+0

哇晚更新,但想說這解決了這個問題。 – Kelly