title: How to remove entity from session when error on Save and the auto-increment Id has not been generated
body: <pre><code>NHibernate version: 3.3.2.
  </code></pre>
  
  <p>I have a NHibernate transaction with several operations. As one of the intermediate steps in the transaction, I try to insert a new entity instance with an auto-increment PK via Session.Save. </p>
  
  <p>In some cases there is an error in the insertion caused by a violated Foreign Key value. I catch the exception in order to make some logging and ignore it, assuming the insert has not been done and going on with the following steps. This is a functional requirement and can not be changed. Validating the values before inserting is not an acceptable solution either, according to performance requirements.</p>
  
  <p>My problem: the non-persisted entity has been added to NH session cache but with an empty Id. So I get this exception later when doing some other NH operations:</p>
  
  <pre><code>NHibernate.AssertionFailure: null id in MyApp.MyPackage.MyClass entry (don't flush the Session after an exception occurs)
  </code></pre>
  
  <p>My question: how can I pull this annoying "zombie" entity off of the session, so I can successfully commit the transaction at the end of all steps? </p>
  
  <p>I can't Evict it, because the Id is empty. </p>
  
  <p>I tried it and got following exception:</p>
  
  <pre><code>System.InvalidOperationException: cannot generate an EntityKey when id is null
  </code></pre>
  
  <p>I can't either <code>Clear()</code> the current session as I am in the middle of a transaction and have some previous and following steps to be preserved. </p>
  
  <p>The original exception I got was somewhat confusing, and was thrown at a different point from where the real problem was originated. This link gave me some information to figure out what was happening: <a href="http://bchavez.bitarmory.com/archive/2007/11/14/nhibernate---null-id-in-entry-dont-flush-the-session.aspx" rel="nofollow">NHibernate - null id in entry (don't flush the Session after an exception occurs)</a></p>