Mapping relations to non-primary keys

In legacy databases, sometimes data is stored in ways that doesn't quite map to an object model. One such scenario is when the relation between for example a Customer and its ContactPersons is controlled by a column other than the primary key in the Customer table. NHibernate provides a way to handle these relations, using the property-ref mapping attribute.

Getting ready

Complete the Getting ready instructions at the beginning of this chapter.

How to do it…

  1. Add a new folder named PropertyRefs to the MappingRecipes project.
  2. Add a class named Customer to the folder:
    using System.Collections.Generic; namespace MappingRecipes.PropertyRefs { public class Customer { public Customer() { ContactPersons=new HashSet<ContactPerson>(); ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.