Search code examples
nhibernatenhibernate-mapping

Mapping a column multiple times in Nhibernate


I have for example an entity. With the following properties:

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

How can I map the CustomerId twice. Once for the int property and once for the many-to-one relationship ?

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

Just this, doesn't work. I've already tried, making them readonly but no success.


Solution

  • One of them should be mapped as readonly (inser/udpate false), and referenced as formula

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

    Then it should be working correctly. Both properties then can be used for Select, Where... order by