O'Reilly logo

NHibernate 3.0 Cookbook by Jason Dentler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bidirectional one-to-many class relationships

In some cases, it's useful to have a bidirectional relationship between entities. In this recipe, I'll show you how to set up a bidirectional one-to-many relationship between two entity classes.

How to do it...

  1. Create an empty class library project named ManualRelationships.
  2. Add a reference to Iesi.Collections.dll in the Lib folder.
  3. Add the following Order class:
    public class Order { public virtual Guid Id { get; protected set; } public Order() { _items = new HashedSet<OrderItem>(); } private ISet<OrderItem> _items; public virtual IEnumerable<OrderItem> Items { get { return _items; } } public virtual bool AddItem(OrderItem newItem) { if (newItem != null && _items.Add(newItem)) { newItem.SetOrder(this); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required