【问题标题】:Nhibernate mapping value object with references as properties将引用作为属性的 Nhibernate 映射值对象
【发布时间】:2014-06-05 06:53:58
【问题描述】:

我有以下实体 CountryStateCity

我也有User 实体。

CountryStateCity 与用户实体没有关系。 UserCountryStateCity 没有直接关系。

我希望能够以某种方式更新用户地址详细信息 用户将选择国家、州和城市并保存这些值。 我正在考虑引入Address 值对象,它将存储此用户地址信息。

这是个好主意吗?您将如何处理这种情况?

我正在使用 nhibernate orm 并使用 Conformist 进行映射(通过代码方法进行映射) 所以我正在考虑将地址映射为值对象

Component(c => c.Address, AddressMap.Mapping());

public class AddressMap
    {
        public static Action<IComponentMapper<Address>> Mapping()
        {
            return c =>
            {
                c.Property(p => p.Country);
                c.Property(p => p.State);                    
                c.Property(p => p.City);
            };
        }
    }

UserMap中有这个

Component(c => c.Address, AddressMap.Mapping());

我收到以下错误

“NHibernate.SessionProvider”的类型初始化器抛出了一个 例外。 {“无法确定类型:Model.Country, ..., for 列:NHibernate.Mapping.Column(Country)"}

UserMap 中没有这行Component(c =&gt; c.Address, AddressMap.Mapping()); 我没有任何错误(我也没有映射地址值对象:)。

【问题讨论】:

    标签: c# .net hibernate nhibernate nhibernate-mapping


    【解决方案1】:

    我想说,这里的问题来自这样一个事实,Country 不会是值类型(包括字符串),而是一个引用。如果是这种情况,我们不能将其映射为c.Property(),而是作为参考:

    c.ManyToOne(p => p.Country, "CountryId"); // reference
    
    c.Property(p => p.State);                 // value types represented 
    c.Property(p => p.City);                  // by values in columns
    

    详情请见:Mapping-by-Code - ManyToOne

    ManyToOne(x => x.PropertyName, m =>
    {
        m.Column("column_name");
        // or...
        m.Column(c =>
        {
            c.Name("column_name");
            // other standard column options
        });
        ...
        // many more
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 2010-09-18
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      相关资源
      最近更新 更多