映射组件集合中的组件

时间:2015-04-30 13:36:51

标签: nhibernate fluent-nhibernate domain-driven-design

我试图映射一个值对象集合,其中包含其他值对象,但我得到以下异常。

nHibernate例外:

 ----> NHibernate.PropertyNotFoundException : Could not find a getter for property '_timeAtAddress' in class 'CustomerAddress'

public class CustomerAddress
{
    private TimePeriod _timeAtAddress;

    protected CustomerAddress() { }

    public CustomerAddress(TimePeriod timeAtAddress)
    {
        _timeAtAddress = timeAtAddress;
    }

    public TimePeriod TimeAtAddress { get { return _timeAtAddress; } }
}

public class TimePeriod
{
    private readonly int _months;
    private readonly int _years;

    protected TimePeriod() { }

    public TimePeriod(int months, int years)
    {
        _months = months;
        _years = years;
    }

    public int Months { get { return _months; } }
    public int Years { get { return _years; } }
}

nHibernate制图:

contact.HasMany<CustomerAddress>(Reveal.Member<Contact>("_customerAddresses"))
    .Schema(...)
    .Table(...)
    .KeyColumn(...)
    .AsBag()
    .Not.LazyLoad()
    .Component(address =>
    {
        .
        .
        .

        address.Component(Reveal.Member<CustomerAddress, TimePeriod>("_timeAtAddress"), timeAtAddress =>
        {
            timeAtAddress.Map(Reveal.Member<TimePeriod>("_years")).Column("TIME_YEARS");
            timeAtAddress.Map(Reveal.Member<TimePeriod>("_months")).Column("TIME_MONTHS");
        });
    });

快速浏览一下Access,但似乎无法找出为组件设置的位置。你能帮忙吗?

2 个答案:

答案 0 :(得分:3)

不是要配置FluentNHibernate来设置私有字段,你不应该告诉它使用构造函数参数吗?

我的直觉是错误就在这里:

address.Component(Reveal.Member<CustomerAddress, TimePeriod>("_timeAtAddress")

您要告诉它使用字段_timeAtAddress

答案 1 :(得分:1)

我设法向前推进(使用私有字段)的唯一方法是设置全局Access.Field约定。

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>() .Conventions.Add(DefaultAccess.Field()))