Fluent NHibernate - 将组件/值类型对象的字典映射为 HasMany

发布于 2024-09-05 00:01:16 字数 1105 浏览 1 评论 0原文

我有一个类 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被放置为字典的键,没有问题。但是,它并未分配给 Rate 中的 Type 属性。

2) 我期望表 InvoiceItem_Rates 中有三列(Item_IdTypeQuantity>Amount 但是,Amount 可疑地不存在,

为什么会发生这些事情?

I have a class, Item that has many Rates. They are keyed by an enum, 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;}
}

I am overriding my mapping thusly:

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();
}

This has two problems with it.

1) When I fetch an item, the Type is placed as the key of the Dictionary without problems. However, it is not assigned to the Type property within the Rate.

2) I'm expecting three columns in the table InvoiceItem_Rates (Item_Id, Type, Quantity, and Amount. However, Amount is suspiciously absent.

Why are these things happening? What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2024-09-12 00:01:16

在我看来,这并不完美,因为枚举键值实际上存储为整数而不是字符串,但可能不是问题。
这里的关键是您不能多次调用 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();            

This isn't perfect in my opinion as the enum key value is actually being stored as an integer instead of a string, but probably isn't an issue.
The key here is that you can't have multiple calls to Component as it's going to overwrite your previous Component call with whatever the last one is.
The correct way to call Component() is as below:

        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();            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文