title: Fluent NHibernate CompositeId trying to insert null values
body: <p>I am working to map an existing database using Fluent NHibernate and have encountered a problem when it comes to complex many-to-many relationships (additional columns). </p>
  
  <p>I know that many-to-many relationships with additional columns have to be mapped as <code>HasMany</code> rather than <code>HasManyToMany</code> as they are not pure many-to-many relationships. The linking table has to be mapped as a class within itself, which I have done in the example below. </p>
  
  <p>When loading this data from an existing database it loads fine. The project I am working on takes this data and inserts it into an empty database, which is where the problem occurs. I think that when inserting into the new database the <code>CompositeId</code> is trying to insert <code>NULL</code> values for <code>ItemID</code> and <code>ItemGroupID</code> which is not allowed in the database. Changing the database structure is not a viable option at this point, is there a way around this issue?</p>
  
  <p>Thanks, example code below.</p>
  
  <hr>
  
  <h2>Entity Classes</h2>
  
  <pre><code>public class Item
  {
      public virtual long ItemID { get; set; }
      public virtual string Name { get; set; }
  }
  
  public class ItemGroup
  {
      public virtual long ItemGroupID { get; set; }
      public virtual string Name { get; set; }
  
      public virtual IList&lt;ItemInGroup&gt; ItemsInGroup { get; set; }
  }
  
  public class ItemInGroup
  {
      public virtual Item Item { get; set; }
      public virtual ItemGroup ItemGroup { get; set; }
      public virtual int? DisplayOrder { get; set; }
  }
  </code></pre>
  
  <h2>Mapping Classes</h2>
  
  <pre><code>public class ItemMap : ClassMap&lt;Item&gt;
  {
      public ItemMap()
      {
          Table("Items");
          Id(x =&gt; x.ItemID).GeneratedBy.Identity();
  
          Map(x =&gt; x.Name);
      }
  }
  
  public class ItemGroupMap : ClassMap&lt;ItemGroup&gt;
  {
      public ItemGroupMap()
      {
          Table("ItemGroups");
          Id(x =&gt; x.ItemGroupID).GeneratedBy.Identity();
  
          Map(x =&gt; x.Name);
          HasMany(x =&gt; x.ItemsInGroup).KeyColumn("ItemGroupID").Cascade.All();
      }
  }
  
  public class ItemInGroupMap : ClassMap&lt;ItemInGroup&gt;
  {
      public ItemInGroupMap()
      {
          Table("ItemsInGroups");
  
          CompositeId().KeyReference(x =&gt; x.Item, "ItemID")
                       .KeyReference(x =&gt; x.ItemGroup, "ItemGroupID");
  
          Map(x =&gt; x.DisplayOrder);
      }
  }
  </code></pre>