title: NHibernate ISession.Save() - Why is this persisting my entities immediately?
body: <p>I am creating a large number of entities with NHibernate, attaching them to my ISession, and then using a transaction to commit my changes to the database. Code sample is below:</p>
  
  <pre><code>ISession _context = SessionProvider.OpenSession();
  
  //Create new entities
  for(int i=0; i&lt;100; i++)
  {
      MyEntity entity = new MyEntity(i);
  
      //Attach new entity to the context
      _context.Save(entity);
  }
  
  //Persist all changes to the database
  using(var tx = _context.BeginTransaction())
  {
      //Flush the session
      tx.Commit();
  }
  </code></pre>
  
  <p>I was under the impression that the line _context.Save() simply makes the ISession aware of the new entity, but that no changes are persisted to the database until I Flush the session via the line tx.Commit().</p>
  
  <p>What I've observed though, is that the database gets a new entity every time I call _context.Save(). I end up with too many individual calls to the database as a result. </p>
  
  <p>Does anyone know why ISession.Save() is automatically persisting changes? Have I misunderstood something about how NHibernate behaves? Thanks.</p>
  
  <p>***EDIT - Just to clarify (in light of the two suggested answers) - my problem here is that the database IS getting updated as soon as I call _context.Save(). I don't expect this to happen. I expect nothing to be inserted into the database until I call tx.Commit(). Neither of the two suggested answers so far helps with this unfortunately. </p>
  
  <p>Some good information on identity generators can be found <a href="http://nhforge.org/blogs/nhibernate/archive/2009/03/20/nhibernate-poid-generators-revealed.aspx" rel="nofollow">here</a></p>