title: Fluent nHibernate - Mapping Children with Composite Keys Yielding Null References
body: <p>Given a simple parent -> child (CK,CK) setup like this .. I am having trouble with adding a new child object and it getting the parent reference. So I would add the object in such a way ..</p>
  
  <pre><code>var parent = new Parent{
    Children = new List&lt;Child&gt;{
     new Child{
       Other = otherReference
     }
    }
  };
  </code></pre>
  
  <p>Or even adding it using the <code>Add()</code> method...</p>
  
  <blockquote>
    <p>parent.Children.Add(new Child { Other = other });</p>
  </blockquote>
  
  <p>The reference to the <code>Parent</code> does not get pushed through. It just ends up as a null property. I get the following exception.</p>
  
  <blockquote>
    <p>{"Cannot insert the value NULL into column 'ParentId', table 'mssql_test.Children'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}</p>
  </blockquote>
  
  <p>I <strong>can</strong> do this ...</p>
  
  <pre><code>new Child { 
     Parent = parentReference,
     Other = otherReference
  }
  </code></pre>
  
  <p>But that seems a bit redundant. My understanding is that it should be able to infer the reference by itself. If this is not possible, perhaps I am just misunderstanding. Can anyone help me? I have an outline of my code below.</p>
  
  <h2>Classes</h2>
  
  <pre><code>class Parent {
    int Id { get; set; }
    IList&lt;Child&gt; Children { get; set; }
  }
  class Other {
    int Id { get; set; }
  }
  class Child {
    Parent Parent { get; set; }
    Other Other { get; set; }
    // other properties
  }
  </code></pre>
  
  <h2>Mapping</h2>
  
  <pre><code> ChildMap() {
        CompositeId()
          .KeyReference(x =&gt; x.Parent, "ParentId")
          .KeyReference(x =&gt; x.Other, "OtherId");
      }
  
      ParentMap(){
       HasManyToMany(x =&gt; x.Children)
                      .AsBag()
                      .ChildKeyColumns.Add(new[] { "ParentId", "OtherId" })
                      .Inverse()
                      .Cascade.All())
                      .Table("[Test]");
  }
  </code></pre>