如何使用Fluent NHibernate映射具有私有作用域的组件?

时间:2011-09-06 10:16:35

标签: nhibernate fluent-nhibernate nhibernate-mapping

我有遗留架构中的列,我想将其映射为组件(也就是值类型)。对组件/值类型的引用属于私有范围。

实体代码如下:

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'

1 个答案:

答案 0 :(得分:-1)

IUserType可以提供帮助,然后您可以在NullSafeGet / set方法中新建Money实例。

there's some links to a money usertype in here.