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

发布于 2024-12-03 01:28:20 字数 1151 浏览 3 评论 0原文

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

实体代码如下所示:

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 技术交流群。

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

发布评论

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

评论(1

空城仅有旧梦在 2024-12-10 01:28:20

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.

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