title: NHibernate - accessing an object's parent via id or object reference
body: <p>I have the following classes:</p>
  
  <pre><code>public class Parent
  {
      public virtual int ParentId { get; set; }
      public virtual string Name { get; set; }
      public virtual ICollection&lt;Child&gt; Children { get; set; }
  }
  
  public class Child
  {
      public virtual int ChildId { get; set; }
      public virtual string Name { get; set; }
      //public virtual int ParentId { get; set; }
      public virtual Parent Parent { get; set; }
  }
  </code></pre>
  
  <p>These classes map to corresponding tables in the database in a one-to-many relationship. In my mapping classes, I do the following:</p>
  
  <pre><code>using FluentNHibernate.Mapping;
  public partial class ParentMap : ClassMap&lt;Parent&gt;
  {
      public ParentMap()
      {
          Id(p =&gt; p.ParentId).Column.("PARENT_ID").GeneratedBy.Native();
          Map(p =&gt; p.Name).Column("PARENT_NAME").Not.Nullable();
          HasMany&lt;Child&gt;(p =&gt; p.Children).Cascade.All().LazyLoad().Inverse().AsSet();
      }
  }
  
  public partial class ChildMap : ClassMap&lt;Child&gt;
  {
      public ChildMap()
      {
          Id(c =&gt; c.ChildId).Column("CHILD_ID").GeneratedBy.Native();
          Map(c =&gt; c.Name).Column("CHILD_NAME").Not.Nullable();
          References&lt;Parent&gt;(c =&gt; x.Parent).Column("PARENT_ID").Not.LazyLoad().Not.Nullable();
      }
  }
  </code></pre>
  
  <p>What I need to be able to do is expose (uncomment the line in the class definition above) the ParentId property in the Child class so that I can do the following:</p>
  
  <pre><code>var child = new Child();
  child.ParentId = 1;
  </code></pre>
  
  <p>That is, I want to be able to attach the Parent to the Child via the child.ParentId property while still being able to access the Parent via the child.Parent property. E.g., </p>
  
  <pre><code>// i currently have to do the following in order to link the child with
  // the parent when I update an existing Child instance (ParentService() and 
  // ChildService() are service classes that sit between my applications and
  // NHibernate).
  var parentService = new ParentService();
  var parent = parentService.GetById(1);
  var child = new Child() { ChildId = 2, Parent = parent, Name = "New Name" };
  var childService = new ChildService();
  childService.Save(child);
  
  // in a different project, i access the Parent object via the child's
  // Parent property
  var childService = new ChildService();
  var child = childService.GetById(2);
  Console.WriteLine(child.Parent.Name);
  
  
  // i want to do this instead
  var child = new Child() { Id = 2, ParentId = 1, Name = "New Name" };
  var childService = new ChildService();
  childService.Save(child);
  Console.WriteLine(child.Id); // 11
  
  // [ ... ]
  
  var childService = new ChildService();
  var child = childService.GetById(2);
  Console.WriteLine(child.Parent.Name);
  </code></pre>
  
  <p>How would I change the mappings to make that happen? TIA,</p>
  
  <p>Ralf Thompson</p>