title: Fluent NHibernate Cascade Issue - Trying To Insert NULL ID
body: <p>I have the following models &amp; mappings (code snippets further below).</p>
  
  <p>One Competition has to have multiple CompetitionAnswers associated with it (multiple choice) from the outset.</p>
  
  <p>At present, using the Fluent NHibernate mappings shown below, when I create a brand new Competition object, populate the properties, then create 3 brand new CompetitionAnswer objects and add them to the CompetitionAnswers property (property on Competition), I would expect to call Save on the session which would INSERT the 1 Competition row and 3 CompetitionAnswer rows to the DB.</p>
  
  <p>However, as soon as I try to call Save on the session, it complains that CompetitionId is null and it can't insert a null into the CompetitionAnswers table for that field - which is right, it shouldn't, however, I assumed that the NHibernate would first create the Competition, then use the newly generated IDENTITY value (CompetitionId) in the CompetitionAnswers table?</p>
  
  <p><strong>Competition (Model)</strong></p>
  
  <pre><code>public virtual int CompetitionId { get; private set; }
  public virtual string Title { get; set; }
  public virtual string Description { get; set; }
  public virtual IList&lt;CompetitionAnswer&gt; CompetitionAnswers { get; set; }
  </code></pre>
  
  <p><strong>CompetitionAnswer (Model)</strong></p>
  
  <pre><code>public virtual int CompetitionAnswerId { get; set; }
  public virtual string Answer { get; set; }
  public virtual Competition Competition { get; set; }
  </code></pre>
  
  <p><strong>CompetitionMap (Fluent NHibernate Mapping)</strong></p>
  
  <pre><code>public CompetitionMap()
  {
      Id(x =&gt; x.CompetitionId)
          .GeneratedBy.Native();
      Map(x =&gt; x.Title);
      Map(x =&gt; x.Description);
      HasMany(x =&gt; x.CompetitionAnswers)
          .Cascade.AllDeleteOrphan()
          .KeyColumn("CompetitionId")
          .Inverse();
      Table("Competitions");
  }
  </code></pre>
  
  <p><strong>CompetitionAnswerMap (Fluent NHibernate Mapping)</strong></p>
  
  <pre><code>public CompetitionAnswerMap()
  {
      Id(x =&gt; x.CompetitionAnswerId)
          .GeneratedBy.Native();
      Map(x =&gt; x.Answer);
      References(x =&gt; x.Competition)
          .Column("CompetitionId");
      Table("CompetitionAnswers");
  }
  </code></pre>
  
  <p>Here is some sample code that I've used to test this scenario, which generates the error:</p>
  
  <pre><code>Competition c = new Competition();
  
  c.Description = "Description";
  c.Title = "Title";
  
  CompetitionAnswer a1 = new CompetitionAnswer { Answer = "Answer 1" };
  CompetitionAnswer a2 = new CompetitionAnswer { Answer = "Answer 2" };
  CompetitionAnswer a3 = new CompetitionAnswer { Answer = "Answer 3" };
  
  c.CompetitionAnswers.Add(a1);
  c.CompetitionAnswers.Add(a2);
  c.CompetitionAnswers.Add(a3);
  
  session.Save(c);
  </code></pre>
  
  <p>The exact error that I get as soon as it tries to Save is:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into
    column 'CompetitionId', table
    'CompetitionAnswers'; column does not
    allow nulls. INSERT fails. The
    statement has been terminated.</p>
  </blockquote>
  
  <p>Can anyone please shed any light on why this isn't currently working?</p>