在组件集合中映射组件

Mapping a Component inside a Component Collection

我正在尝试映射一个值对象集合,其中包含其他值对象,但出现以下异常。

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,但似乎无法弄清楚在哪里为组件设置它。你能帮忙吗?

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

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

与其配置 FluentNHibernate 来设置私有字段,不如告诉它使用构造函数参数?

我的直觉是错误在这里:

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

您告诉它使用字段的位置 _timeAtAddress