如何使用 Fluent NHibernate 映射具有私有作用域的组件?
我有遗留模式中的列,我想将其映射为组件(也称为值类型)。对组件/值类型的引用属于私有范围。
实体代码如下所示:
public class Allocation : Entity<int>
{
//...
private readonly Money price;
protected Allocation() {} /* for NH only */
public Allocation(/* ... */ Money price)
{
//...
this.price = price;
}
}
值类型代码如下所示:
public struct Money
{
private readonly decimal amount;
private readonly int currencyId;
public Money(decimal amount, int currencyId)
{
this.amount = amount;
this.currencyId = currencyId;
}
}
当前映射如下所示:
Component(Reveal.Member<Allocation, Money>("price"),
p =>
{
p.Map(Reveal.Member<Money>("amount")).Column("CURRENCY_PRICE").Not.Nullable();
p.Map(Reveal.Member<Money>("currencyId")).Column("CURRENCY").Not.Nullable();
});
目前,上面的代码抛出以下异常:
System.ArgumentException : Expression of type 'System.Object' cannot be used for return type 'BI.IPM.Services.TradeAllocation.Domain.Entities.Money'
I've got to columns in a legacy schema, that I'd like to map as a component (aka value type). The reference to the component/value type is of private scope.
The entity code looks like so:
public class Allocation : Entity<int>
{
//...
private readonly Money price;
protected Allocation() {} /* for NH only */
public Allocation(/* ... */ Money price)
{
//...
this.price = price;
}
}
The value type code looks like so:
public struct Money
{
private readonly decimal amount;
private readonly int currencyId;
public Money(decimal amount, int currencyId)
{
this.amount = amount;
this.currencyId = currencyId;
}
}
The current mapping looks like so:
Component(Reveal.Member<Allocation, Money>("price"),
p =>
{
p.Map(Reveal.Member<Money>("amount")).Column("CURRENCY_PRICE").Not.Nullable();
p.Map(Reveal.Member<Money>("currencyId")).Column("CURRENCY").Not.Nullable();
});
Currently, the code above throws the following exception:
System.ArgumentException : Expression of type 'System.Object' cannot be used for return type 'BI.IPM.Services.TradeAllocation.Domain.Entities.Money'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IUserType 可以提供帮助,然后您可以在 NullSafeGet/set 方法中新建 Money 实例。
这里有一些指向资金用户类型的链接。
an IUserType could help, you could then new up the Money instance in the NullSafeGet/set Methods.
there's some links to a money usertype in here.