Book Image

Learning NHibernate 4

Book Image

Learning NHibernate 4

Overview of this book

Table of Contents (18 chapters)
Learning NHibernate 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Mapping associations


Association between two classes is a very simple concept to understand. If you want to have association between ClassA and ClassB, then you can add a property of type ClassB on ClassA. This is the most basic form of association.

Associations come in four different forms as described next:

  • One-to-many: One end of the association has single instance of the entity while the other end has multiple instances. In code, this is represented by a property of the Collection type. For example, IList<T>, [], ICollection<T> ,IEnumerable<T>. and so on

  • Many-to-one: Opposite of one-to-many.

  • One-to-one: Both ends of associations have one single instance of entities.

  • Many-to-many: Both ends of associations have multiple instances of entities.

We have example of all associations except many-to-many in our domain. Let me refer to part of the Employee class and the Benefit class as follows:

public class Employee : EntityBase
{	
  public virtual ICollection<Benefit>...