title: nHibernate: identifier of an instance of <some type> was altered from X to X
body: <p>When I do a post to this
  endpoint, I get the error: "identifier of an instance of PupilEntity was altered from 100021823 to 100021823."  Why?  I believe the code below only does a read.<br>
  Is there a write I'm not seeing? I am using web api and nHibernate.  Perhaps it's not the pupilId at all and instead there's something wrong with how
  we set up the transaction and then try to commit/rollback.  Note that the transaction is setup and finalized in an ActionFilterAttribute.  However, I'm new to .net stack, so not sure what it is.  </p>
  
  <pre><code>public class HibernateSessionFilter : ActionFilterAttribute
  {
  
      public override void OnActionExecuting(HttpActionContext actionContext)
      {
          //Debug.WriteLine("Opening Hibernate Session.");
          CurrentSessionContext.Bind(HibernateConfig.SessionFactory.OpenSession());
  
          //Only apply transaction for post or put requests
          string methodType = actionContext.Request.Method.Method;
  
          if (methodType.ToUpper().Equals(HttpVerbs.Post.ToString().ToUpper())
              || methodType.ToUpper().Equals(HttpVerbs.Put.ToString().ToUpper()))
          {
              if (HibernateConfig.SessionFactory.GetCurrentSession() != null)
                  HibernateConfig.SessionFactory.GetCurrentSession().BeginTransaction();
          }
      }
  
      public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
      {
          ISession session = CurrentSessionContext.Unbind(HibernateConfig.SessionFactory);
          try
          {
              if (session.Transaction.IsActive)
                  if (actionExecutedContext.Exception != null)
                      session.Transaction.Rollback();
                  else
                      session.Transaction.Commit();
          }
          finally
          {
              session.Transaction.Dispose();
              //session.Flush();
              session.Close();
              session.Dispose();
          }
          //Debug.WriteLine("Closed Hibernate Session.");
      }
  }
  
      //Marks a pupil as withdrawn
      [ActionName("WithdrawPupil")]
      [HttpPost]
      public WithdrawPupilResponse WithdrawPupil(int id, [FromBody] WithdrawPupilRequest reqData)
      {
          int code = reqData.code;
  
          try
          {
              HibernateConfig.SessionFactory.GetCurrentSession().Get&lt;PupilEntity&gt;(id);
              return new WithdrawPupilResponse(true, PUPIL_WITHDRAWN_MSG);
          }
          catch (InvalidValueException ex)
          {
              string message = String.Format(WITHDRAW_PUPIL_ERROR_MSG, id, ex.Message);
              throw new ClientsFaultException(message, NewMsmiErrorCodes.WITHDRAW_PUPIL_ERROR, ex);
          }
          catch (Exception ex)
          {
              throw new ServersFaultException(string.Format("Unexpected Problem withdrawing pupil {0}.", id), 1234L, ex);
          }
      }
  }
  
  
  public class WithdrawPupilResponse
  {
      private Boolean success { get; set; }
      private string message { get; set; }
  
      public WithdrawPupilResponse(Boolean success, string message)
      {
          this.success = success;
          this.message = message;
      }
  }    
  </code></pre>
  
  <p>Here are the logs from hibernate:</p>
  
  <pre><code>2014-11-22 00:25:29,888 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl [session-id=a0d4aaf3-bbfa-4c2c-a2e2-ae6c0db424ba] opened session at timestamp: 635522127298, for session factory: [/2d25c8509fe643b18a8526e5bd764d33]
  2014-11-22 00:25:29,893 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction Begin (Unspecified)
  2014-11-22 00:25:29,894 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Connection.DriverConnectionProvider Obtaining IDbConnection from Driver
  2014-11-22 00:25:29,904 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.DefaultLoadEventListener loading entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,905 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.DefaultLoadEventListener attempting to resolve: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,907 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.DefaultLoadEventListener object not resolved in any cache: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,908 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister Fetching entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,909 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader loading entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,928 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Opened new IDbCommand, open IDbCommands: 1
  2014-11-22 00:25:29,929 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Building an IDbCommand object for the SqlString: SELECT pupilentit0_.PupilID as PupilID2_0_, pupilentit0_.CurrentMonthStatus as CurrentM2_2_0_, pupilentit0_.Status as Status2_0_, pupilentit0_.LastUpdated as LastUpda4_2_0_, pupilentit0_.Active as Active2_0_ FROM Foo.dbo.Pupils pupilentit0_ WHERE pupilentit0_.PupilID=?
  2014-11-22 00:25:29,930 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.Int32Type binding '100021823' to parameter: 0
  2014-11-22 00:25:29,932 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Loader.Loader SELECT pupilentit0_.PupilID as PupilID2_0_, pupilentit0_.CurrentMonthStatus as CurrentM2_2_0_, pupilentit0_.Status as Status2_0_, pupilentit0_.LastUpdated as LastUpda4_2_0_, pupilentit0_.Active as Active2_0_ FROM Foo.dbo.Pupils pupilentit0_ WHERE pupilentit0_.PupilID=@p0
  2014-11-22 00:25:29,937 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.SQL
      SELECT
          pupilentit0_.PupilID as PupilID2_0_,
          pupilentit0_.CurrentMonthStatus as CurrentM2_2_0_,
          pupilentit0_.Status as Status2_0_,
          pupilentit0_.LastUpdated as LastUpda4_2_0_,
          pupilentit0_.Active as Active2_0_
      FROM
          Foo.dbo.Pupils pupilentit0_
      WHERE
          pupilentit0_.PupilID=@p0;
      @p0 = 100021823 [Type: Int32 (0)]
  2014-11-22 00:25:29,938 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction Enlist Command
  2014-11-22 00:25:29,942 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher ExecuteReader took 3 ms
  2014-11-22 00:25:29,942 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Opened IDataReader, open IDataReaders: 1
  2014-11-22 00:25:29,943 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader processing result set
  2014-11-22 00:25:29,944 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader result set row: 0
  2014-11-22 00:25:29,945 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader result row: EntityKey[Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,948 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader Initializing object from DataReader: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,951 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister Hydrating entity: [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,952 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.StringType returning '-' as column: CurrentM2_2_0_
  2014-11-22 00:25:29,953 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.StringType returning 'Bill overdue or extension exhausted' as column: Status2_0_
  2014-11-22 00:25:29,953 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.DateTimeType returning '1/16/2014 10:44:22 AM' as column: LastUpda4_2_0_
  2014-11-22 00:25:29,954 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Type.BooleanType returning 'True' as column: Active2_0_
  2014-11-22 00:25:29,955 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader done processing result set (1 rows)
  2014-11-22 00:25:29,955 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Driver.NHybridDataReader running NHybridDataReader.Dispose()
  2014-11-22 00:25:29,956 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Closed IDataReader, open IDataReaders :0
  2014-11-22 00:25:29,956 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher DataReader was closed after 17 ms
  2014-11-22 00:25:29,957 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher Closed IDbCommand, open IDbCommands: 0
  2014-11-22 00:25:29,958 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader total objects hydrated: 1
  2014-11-22 00:25:29,959 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.TwoPhaseLoad resolving associations for [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,962 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.Loading.LoadContexts creating collection wrapper:[Foo.NewMSMI.Models.Entities.PupilEntity.PupilWithdrawal#100021823]
  2014-11-22 00:25:29,967 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.TwoPhaseLoad done materializing entity [Foo.NewMSMI.Models.Entities.PupilEntity#100021823]
  2014-11-22 00:25:29,967 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Engine.StatefulPersistenceContext initializing non-lazy collections
  2014-11-22 00:25:29,968 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Loader.Loader done entity load
  2014-11-22 00:25:30,015 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction Start Commit
  2014-11-22 00:25:30,016 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener flushing session
  2014-11-22 00:25:30,017 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener processing flush-time cascades
  2014-11-22 00:25:30,019 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Foo.NewMSMI.Models.Entities.PupilEntity
  2014-11-22 00:25:30,021 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Foo.NewMSMI.Models.Entities.PupilEntity.PupilWithdrawal
  2014-11-22 00:25:30,022 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade done cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for collection: Foo.NewMSMI.Models.Entities.PupilEntity.PupilWithdrawal
  2014-11-22 00:25:30,022 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] INFO  NHibernate.Engine.Cascade done processing cascade NHibernate.Engine.CascadingAction+SaveUpdateCascadingAction for: Foo.NewMSMI.Models.Entities.PupilEntity
  2014-11-22 00:25:30,023 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener dirty checking collections
  2014-11-22 00:25:30,024 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Event.Default.AbstractFlushingEventListener Flushing entities and processing referenced collections
  2014-11-22 00:25:33,005 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Transaction.AdoTransaction IDbTransaction disposed.
  2014-11-22 00:25:33,005 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl transaction completion
  2014-11-22 00:25:33,006 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.ConnectionManager aggressively releasing database connection
  2014-11-22 00:25:33,006 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Connection.ConnectionProvider Closing connection
  2014-11-22 00:25:33,008 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl closing session
  2014-11-22 00:25:33,008 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.AdoNet.AbstractBatcher running BatcherImpl.Dispose(true)
  2014-11-22 00:25:33,011 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl [session-id=a0d4aaf3-bbfa-4c2c-a2e2-ae6c0db424ba] running ISession.Dispose()
  2014-11-22 00:25:33,011 theClientId 99eb1769-766d-4710-99cd-db2b64a5adc4 [9] DEBUG NHibernate.Impl.SessionImpl [session-id=a0d4aaf3-bbfa-4c2c-a2e2-ae6c0db424ba] executing real Dispose(True)
  NHibernate.HibernateException: identifier of an instance of Foo.NewMSMI.Models.Entities.PupilEntity was altered from 100021823 to 100021823
     at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode)
     at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session)
     at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
     at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event)
     at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
     at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
     at NHibernate.Impl.SessionImpl.Flush()
     at NHibernate.Transaction.AdoTransaction.Commit()
     at Foo.NewMSMI.Filters.HibernateSessionFilter.OnActionExecuted(HttpActionExecutedContext actionExecutedContext) in C:\Users\thebiglebowski\source\Foo.NET\dotnet\Foo.NewMSMI\Filters\HibernateSessionFilter.cs:line 43
  </code></pre>