How do I map a n-column primary key with nHibernate

Elad Benda picture Elad Benda · Nov 25, 2011 · Viewed 15.8k times · Source

I have a table with 2 columns as PK (composite primary key).

How can I map them to "Id" in hbm.xml?

<id name="A" />  

How can I do it with fluent nhibernate?

Answer

Gerard picture Gerard · Nov 25, 2011

The NHibernate documentation describes how to use and map a composite-id.

You can also use a component as compositeid.

And for Fluent NHibernate:

public class ClassNameMap: ClassMap<ClassName>
{
    public ClassNameMap()
    {
        CompositeId().
            .KeyReference(x => x.A, "A")
            .KeyReference(x => x.B, "B");
    }
}