【问题标题】:Mapping a column multiple times in Nhibernate在 Nhibernate 中多次映射列
【发布时间】:2012-12-21 11:29:03
【问题描述】:

例如,我有一个实体。具有以下属性:

public class Entity
{
   public int CustomerId { get; set; }
   public Customer { get; set; } 
}

如何将 CustomerId 映射两次。一次用于 int 属性,一次用于多对一关系?

<many-to-one name="Customer" column="[CustomerId]" class="Customer"/>
<property name="CustomerId" column="[CustomerId]" type="Int64" />

仅此而已,行不通。我已经尝试过,将它们设为只读但没有成功。

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

    其中一个应该被映射为只读(inser/udpate false),并引用为formula

    <many-to-one name="Customer" column="[CustomerId]" class="Customer"/>
    <property name="CustomerId" formula="[CustomerId]" type="Int64" insert="false" update="false" />
    

    那么它应该可以正常工作。然后这两个属性都可以用于 Select, Where... order by

    【讨论】:

      【解决方案2】:

      你不需要映射CustomerId,你可以通过Customer.CustomerId来访问。如果您使用延迟加载,CustomerId 将填充到代理对象中,因此它始终可用,而不会触发额外的选择。

      如果您绝对必须公开它,请将其公开为可为空的只读属性:

         public Customer { get; set; } 
         public int? CustomerId
         {
             get { return Customer == null ? (int?)null: Customer.CustomerId }
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        相关资源
        最近更新 更多