title: Steps that NHibernate takes to insert new id into database using sys.sequences
body: <p>I'm trying to figure out how NHibernate is assigning ids to my game project that I inherited from a previous developer.</p>
  
  <p>When a new photo is uploaded from the web, the API performs this action:</p>
  
  <pre><code>children.Add(e);
  return Session.Save(e);
  </code></pre>
  
  <ul>
  <li>where children = array of photo objects{id, title, size}</li>
  <li>where e = new photo object being uploaded and added to the array of
  children</li>
  </ul>
  
  <p>Session is an NHibernate session.</p>
  
  <p>There is a database table for the photos for with columns of id, title, and size.</p>
  
  <p>The id column uses a sequence in sys.sequences for the ids, it is not an identity column. The name of the sequence is: nextGamePhotoId</p>
  
  <p>When I run Visual Studio in debug mode and step through each process, I see this:</p>
  
  <ul>
  <li><p>Before the Session.Save() call, the new photo object, e, has an id of
  zero.</p></li>
  <li><p>After the Session.Save() call, e has a new id that is equal to the current_value + 1 from the sys.sequences table.</p></li>
  </ul>
  
  <p>Despite running through my debugger, I can't figure out how or where NHibernate assigns the "id".</p>
  
  <p>The photo class is mapped using NHibernate.Mapping.ByCode , like this:</p>
  
  <pre><code> public class GamePhotoMapping : ClassMapping&lt;GamePhoto&gt;
      {
          public GamePhotoMapping()
          {
              Schema("game");
              this.EnhancedSequenceId(x =&gt; x.Id, "nextGamePhotoId");
              Property(x =&gt; x.Title, gp =&gt; gp.NotNullable(true));
              Property(x =&gt; x.size);
          }
      }
  </code></pre>
  
  <p>This is the only place in the code where I see a reference to the sequence(nextGamePhotoId).</p>
  
  <p>So I guess NHibernate is doing something behind the scenes?</p>
  
  <p>Does anyone know how exactly is NHibernate getting and assigning the id?</p>
  
  <p>Thanks!</p>