Mapping a many-to-many relationship

A student often attends many classes and hopefully every class is attended by more than one student. This type of relationship is called many-to-many and in a relational database, an intermediate table usually represents it, with at least two columns referencing the keys of the participating entities.

NHibernate supports many-to-many relationships and does so without having to expose the intermediate table to the code.

Getting ready

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

How to do it…

  1. Create a new folder named ManyToMany in the MappingRecipes project.
  2. Add a new class Student to the folder:
    public class Student { public virtual Guid Id { get; protected set; } public virtual string ...

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.