title: Why am I getting this nhibernate NonUniqueObjectException?
body: <p>The following method queries my database, using a new session.  If the query succeeds, it attaches (via "Lock") the result to a "MainSession" that is used to support lazy loading from a databound WinForms grid control.</p>
  
  <p>If the result is already in the MainSession, I get the exception:</p>
  
  <p><strong>NHibernate.NonUniqueObjectException : a different object with the same identifier value was already associated with the session: 1, of entity: BI_OverlordDlsAppCore.OfeDlsMeasurement</strong></p>
  
  <p>when I attempt to re-attach, using the Lock method.</p>
  
  <p>This happens even though I evict the result from the MainSession before I attempt to re-attach it.</p>
  
  <p>I've used the same approach when I update a result, and it works fine.</p>
  
  <p>Can anyone explain why this is happening?</p>
  
  <p>How should I go about debugging this problem?</p>
  
  <pre><code>    public static OfeMeasurementBase GetExistingMeasurement(OverlordAppType appType, DateTime startDateTime, short runNumber, short revision)
      {
          OfeMeasurementBase measurement;
  
          var mainSession = GetMainSession();
  
          using (var session = _sessionFactory.OpenSession())
          using (var transaction = session.BeginTransaction())
          {
              // Get measurement that matches params
              measurement =
                  session.CreateCriteria(typeof(OfeMeasurementBase))
                         .Add(Expression.Eq("AppType", appType))
                         .Add(Expression.Eq("StartDateTime", startDateTime))
                         .Add(Expression.Eq("RunNumber", runNumber))
                         .Add(Expression.Eq("Revision", revision))
                         .UniqueResult() as OfeMeasurementBase;
  
              // Need to evict from main session, to prevent potential 
              // NonUniqueObjectException if it's already in the main session
              mainSession.Evict(measurement);
  
              // Can't be attached to two sessions at once
              session.Evict(measurement);
  
              // Re-attach to main session
              // Still throws NonUniqueObjectException!!!
              mainSession.Lock(measurement, LockMode.None);
  
              transaction.Commit();
          }
  
          return measurement;
      }
  </code></pre>