Mapping inheritance
You will often have classes in your domain model that inherit from other classes. In our domain model, we have all classes representing a benefit inherited from the Benefit
class. Following is a slimmed down version of the class diagram from our domain model representing this inheritance relationship:
It is possible to map these classes to database tables as you would map any other class. But there are situations when you would want NHibernate to do more for you out of the box. One such situation involves polymorphic associations.
Polymorphic associations are associations where one of the ends is declared to be of base class type, but at runtime it holds instance of one of the derived types. For instance, the association from Employee
to Benefit
class in our domain model is an example of polymorphic association:
public class Employee : EntityBase { public virtual ICollection<Benefit> Benefits { get; set; } }
Here, though the collection property Benefits
declared on...