繁体   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