在Nhibernate中多次映射列

时间:2012-12-21 11:29:04

标签: nhibernate nhibernate-mapping

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

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" />

就是这样,不起作用。我已经尝试过了,只能让它们读取但没有成功。

2 个答案:

答案 0 :(得分:3)

其中一个应该映射为只读(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

答案 1 :(得分:1)

您无需映射CustomerId,您可以通过Customer.CustomerId访问它。如果您正在使用延迟加载,则会在代理对象中填充CustomerId,因此它始终可用,而不会触发其他选择。

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

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