2009-07-10 74 views
5

我有這些類:如何使用/映射數據庫視圖來填充包含的集合?

public class FloorFill 
{ 
    protected FloorFill(){} 
    public virtual ProductCatalog Catalog { get; set; } 
    public virtual Inventory BatchedItem { get; set; } 
    public virtual Transaction Batch { get; set; } 
    public virtual int ItemReference { get; set; } 
    public virtual IList<InventoryLocation> BackstockLocations { get; set; } 
} 
public class InventoryLocation 
{ 
    public InventoryLocation(){} 
    public virtual int Id { get; set; } 
    public virtual int ItemReference { get; private set; } 
    public virtual Location Where { get; set; } 
    public virtual int HowMany { get; set; } 
} 

我有因位置和一些其它過濾聚集Items數據庫視圖。我想在映射中引用這個視圖來填充FloorFill.BackstockLocations集合。

我應該用什麼方法來使這個集合填充?我想收集到懶惰的負載,但在這一點上,我只是很樂意獲取數據。

以下是映射文件:

public class FloorFillMap : EntityBaseMap<FloorFill> 
{   
    public FloorFillMap() 
    { 
     Map(x => x.ItemReference); 
     References(x => x.Catalog, "ProductCatalogId") 
         .WithForeignKey(); 
     References(x => x.Batch, "TransactionId") 
       .WithForeignKey() 
       .Cascade.SaveUpdate(); 
     References(x => x.BatchedItem, "InventoryId") 
       .WithForeignKey() 
       .Cascade.SaveUpdate(); 
     HasMany(x => x.BackstockLocations) 
      .KeyColumnNames.Add("ItemReference") 
      .Inverse() 
      .Cascade.None() 
      .LazyLoad(); 
    } 
} 

public class InventoryLocationMap : ClassMap<InventoryLocation> 
{ 
    public InventoryLocationMap() 
    { 
     WithTable("InventoryLocations"); 
     Id(x => x.Id); 
     References(x => x.Where, "LocationId") 
      .FetchType.Join() 
      .Cascade.None(); 
     Map(x => x.HowMany); 
     ReadOnly(); 
    } 
} 

結果查詢是:

SELECT 
    this_.Id as Id33_0_, 
    this_.Created as Created33_0_, 
    this_.ItemReference as ItemRefe3_33_0_, 
    this_.Modified as Modified33_0_, 
    this_.RowVersion as RowVersion33_0_, 
    this_.ProductCatalogId as ProductC6_33_0_, 
    this_.TransactionId as Transact7_33_0_, 
    this_.InventoryId as Inventor8_33_0_ 
FROM [FloorFill] this_ 

回答

4

映射一個觀點是相同的映射表,只要你不嘗試更新它。

你在這份聲明中想要做什麼?

References(x => x.Where, "LocationId") 
    .FetchType.Join().WithColumns("Id").Cascade.None(); 

"LocationId"是關鍵列名,但WithColumns通話將覆蓋該值。

發生什麼或不發生什麼的錯誤或其他指示將有所幫助。

+0

我刪除了WithColumns(「Id」)。 select語句將InventoryLocations.ItemRefence與FloorFill.Id進行比較,我需要將其映射到FloorFill.ItemRefence。 – Barry 2009-07-13 14:36:23

+0

您不應該有WithTableName,可以從其他實體的地圖中確定。在您的InventoryLocationMap中,您應該刪除您的Id上的ColumnName,因爲它是主鍵,並且因爲您將外鍵映射爲屬性,所以刪除Map(x => x.ItemReference)。現在會發生什麼? – 2009-07-13 17:43:53

+0

我編輯了代碼清單,以反映我對您建議的更改的理解。這不會創建一個Backstock位置集合。 – Barry 2009-07-15 17:05:50