title: Fluent NHibernate does an update after an insert on a Composite Key
body: <p>I am using Fluent NHibernate to map a composite key. I need the child entity to receive the newly inserted parent key when the parent is created. This works but NHibernate is doing an insert on the child and thereafter doing an update on one of the composite key IDs on the child.   </p>
  
  <p>The entity relationships are as follows:  </p>
  
  <p>1) Model (parent) - has many FactorWeights.<br>
  2) FactorWeight (child) has one Factor.<br>
  3) Factors can have different weightings per model.  </p>
  
  <p>Since Models have many Factors and the Factor can have different weightings for different models, the FactorWeight entity is required with the composite key of ModelID and FactorID.</p>
  
  <p>Model:  </p>
  
  <pre>
      public virtual int ID { get; set; }  
      public virtual IList&lt;FactorWeight> FactorWeights { get; set; }  
      public virtual string Name { get; set; }    
  </pre>
  
  <p>Factor:</p>
  
  <pre>
      public virtual int ID { get; set; }  
      public virtual string Name { get; set; }  
  </pre>
  
  <p>FactorWeight:</p>
  
  <pre>
      public virtual Factor Factor { get; set; }  
      public virtual Model Model { get; set; }  
      public virtual decimal Weight { get; set; }  
  </pre>
  
  <p>The mappings are as follows:  </p>
  
  <p>Model:</p>
  
  <pre>
  public void Override(AutoMapping&lt;Model> mapping)
  {
   mapping.Id(x => x.ID);
   mapping.Map(x => x.Name);
  
   mapping.HasMany(x => x.FactorWeights)
      .KeyColumn("ModelID");
  }
  </pre>
  
  <p>FactorWeights</p>
  
  <pre>
  public void Override(AutoMapping&lt;FactorWeight> mapping)
  {
   mapping.CompositeId()
             .KeyReference(factorWeight => factorWeight.Model, "ModelID")
             .KeyReference(factorWeight => factorWeight.Factor, "FactorID");
  
   mapping.Map(factorWeight => factorWeight.Weight);
  }
  </pre>
  
  <p>I use the following code to create the FactorWeight within the Model object:</p>
  
  <pre>
  Model.FactorWeights.Add(new FactorWeight {Factor = factor, Weight = weighting, Model = Model });
  </pre>
  
  <p>The following is used to persist the entity (where the model is passed to the method):</p>
  
  <pre>
  public void CreateEntity(object entity)
  {
   HibernateConfiguration.Session.Transaction.Begin();
   NHibernateConfiguration.Session.Save(entity);   
   NHibernateConfiguration.Session.Transaction.Commit();
  }
  </pre>
  
  <p>The Model entity is saved without an error as well as the FactorWeight. The problem is that SQL Profiler shows an update on the FactorWeight.ModelID column immediately after the insert. NHibernate updates the FactorWeight.ModelID with the newly inserted ModelID which was already present in the insert.  </p>
  
  <p>SQl Profiler Trace:</p>
  
  <pre>
  exec sp_executesql N'INSERT INTO [FactorWeight] (Weight, ModelID, FactorID) VALUES (@p0, @p1, @p2)',N'@p0 decimal(1,0),@p1 int,@p2 int',@p0=1,@p1=56,@p2=1  
  exec sp_executesql N'UPDATE [FactorWeight] SET ModelID = @p0 WHERE ModelID = @p1 AND FactorID = @p2',N'@p0 int,@p1 int,@p2 int',@p0=56,@p1=56,@p2=1
  </pre>
  
  <p>I assume this is a problem with the mapping of the composite key.</p>
  
  <p>I am using version 2.1.2.400 of NHibernate and version 1.1.0.685 of FluentNHibernate.</p>
  
  <p>Thank you.</p>