title: NHibernate: Problem trying to save an entity containing collections
body: <p>I need some help.</p>
  
  <p>I'm just starting out with NHibernate and I'm using Fluent for mappings. Everything seemed to work fine until today.
  Here is the story:</p>
  
  <p>I have two tables in my db: <strong>Store</strong> and <strong>WorkDay</strong>
  The first table contains info about the store, and the <strong>WorkDay</strong> table contains info about the days of week and start/end time when the store is open.
  <strong>Store</strong> contains a Guid <strong>StoreID</strong> PK column that is referenced in the <strong>WorkDay</strong> table.
  So I have a mapping file for <strong>Store</strong> where I have a <strong>HasMany</strong> association with the <strong>WorkDay</strong> table, and a corresponding POCO for <strong>Store</strong>.
  Now, when I fill in all the necessary data and try to persist it to database, I get an exception telling me that the insert into table <strong>WorkDay</strong> failed because the <strong>StoreID</strong> had null value and the table constraint doesn't allow nulls for that column (which is, of course, expected behavior).</p>
  
  <p>I understand the reason for this exception, but I don't know how to solve it.
  The reason why the insert fails is because the <strong>StoreID</strong> gets generated upon insert, but the [b]WorkDay[/b] collection <strong>gets saved first</strong>, in the time when the <strong>StoreID</strong> hasn't yet been generated!</p>
  
  <p>So, how do I force NHibernate to generate this ID to pass it to dependent tables? Or is there another solution for this?</p>
  
  <p>Thank you!</p>
  
  <p>Here's the code for StoreMap</p>
  
  <pre><code>public class StoreMap : ClassMap&lt;Store&gt; {
      public StoreMap() {
          Id(x =&gt; x.StoreID)
              .GeneratedBy.GuidComb();
          Map(x =&gt; x.City);
          Map(x =&gt; x.Description);
          Map(x =&gt; x.Email);
          Map(x =&gt; x.Fax);
          Map(x =&gt; x.ImageData).CustomType("BinaryBlob");
          Map(x =&gt; x.ImageMimeType);
          Map(x =&gt; x.Name);
          Map(x =&gt; x.Phone);
          Map(x =&gt; x.Street);
          Map(x =&gt; x.Zip);
  
          HasMany(x =&gt; x.WorkDays)
              .Inverse().KeyColumn("StoreID").ForeignKeyCascadeOnDelete()
              .Cascade.All();
      }
  }
  </code></pre>
  
  <p>and this is for the WorkDayMap</p>
  
  <pre><code>public class WorkDayMap : ClassMap&lt;WorkDay&gt;{
      public WorkDayMap() {
          Id(x =&gt; x.WorkDayID)
              .GeneratedBy.Identity();
          Map(x =&gt; x.TimeOpen);
          Map(x =&gt; x.TimeClose);
          References(x =&gt; x.Store).Column("StoreID");
          References(x =&gt; x.Day).Column("DayID");
      }
  }
  </code></pre>