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…
Add a new folder named
PropertyRefs
to theMappingRecipes
project.Add a class named
Customer
to the folder:using System.Collections.Generic; namespace MappingRecipes.PropertyRefs { public class Customer { public Customer() { ContactPersons=new HashSet<ContactPerson>(); } public virtual int Id { get; protected set; } public virtual string Name { get; set; } public virtual ISet<ContactPerson> ContactPersons { get; set; } public virtual int...