簡體   English   中英

Fluent NHibernate - 將組件/值類型對象的字典映射為HasMany

[英]Fluent NHibernate - Mapping a dictionary of component/value type objects as a HasMany

我有一個班級, Item有很多Rates 它們由枚舉RateType

public class Item
{
    int Id {get;set;}
    IDictionary<RateType, Rate> Rates {get;set;}
    // some other stuff
}

public class Rate
{
    RateType Type {get;set;}
    decimal Amount {get;set;}
    decimal Quantity {get;set;}
}

我這樣覆蓋了我的映射:

public void Override(FluentNHibernate.Automapping.AutoMapping<Item> mapping)
{
    mapping.HasMany(x => x.Rates)
        .AsMap(x => x.Type)
        .KeyColumns.Add("Item_Id")
        .Table("InvoiceItem_Rates")
        .Component(x => x.Map(r => r.Amount))
        .Component(x => x.Map(r => r.Quantity))
        .Cascade.AllDeleteOrphan()
        .Access.Property();
}

這有兩個問題。

1)當我獲取一個項目時, Type被放置為Dictionary的鍵而沒有問題。 但是,它未分配給Rate內的Type屬性。

2)我期待表InvoiceItem_RatesItem_IdTypeQuantityAmount )中的三列。但是, Amount可疑地缺席。

為什么會發生這些事情? 我究竟做錯了什么?

這在我看來並不完美,因為枚舉鍵值實際上存儲為整數而不是字符串,但可能不是問題。 這里的關鍵是你不能對Component進行多次調用,因為它會覆蓋你之前的Component調用,無論最后一個是什么。 調用Component()的正確方法如下:

        Id(x => x.Id);
        HasMany(x => x.Rates)
            .AsMap(x => x.Type)
            .KeyColumn("Item_Id")
            .Table("InvoiceItem_Rates")
            .Component(x =>
                           {
                               x.Map(r => r.Amount);
                               x.Map(r => r.Quantity);
                           })
            .Cascade.AllDeleteOrphan();            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM