Fluent NHibernate - 将组件/值类型对象的字典映射为 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
被放置为字典的键,没有问题。但是,它并未分配给 Rate
中的 Type
属性。
2) 我期望表 InvoiceItem_Rates
中有三列(Item_Id
、Type
、Quantity
和 >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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,这并不完美,因为枚举键值实际上存储为整数而不是字符串,但可能不是问题。
这里的关键是您不能多次调用 Component,因为它将用最后一个调用覆盖您之前的 Component 调用。
调用 Component() 的正确方法如下:
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: