Search code examples
.netnhibernatefluent-nhibernatecomposite-primary-key

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


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?


Solution

  • 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");
        }
    }