title: Why is Session.Evict in OnPostUpdate causing "Possible nonthreadsafe access to session" exception?
body: <p>I have a user that has a number of roles. The user is linked to the roles using a link entity table. I have set the configuration file to cascade delete the user role link entities when a user is deleted.</p>
  
  <p>We are currently using soft delete to delete entities. We have added a soft delete event listener that is triggered by a delete. When an entity is being deleted, it triggers the <code>DeleteEntity</code> event which marks the entity as deleted.</p>
  
  <p>We also have an override the <code>OnPostUpdate</code> event to remove the entities from the cache by calling Evict on the entity.</p>
  
  <p>If I create a user without any roles, then delete it, everything works fine (it also works if cascade disabled). However if I have a user with at least one role assigned and I delete the user, after the call to Evict in <code>OnPostUpdate</code>, I get a NHibernate exception "NHibernate.AssertionFailure: Possible nonthreadsafe access to session".</p>
  
  <p>I have tried, in <code>OnPostUpdate</code>, to use the child session to Evict the entity, the exception is not thrown, however, the entity is not evicted.</p>
  
  <pre><code>public void UserDelete(.....)
  {
      var user = repository.Fetch&lt;User&gt;(id);
  
      repository.Remove(user);
      repository.Connection.Commit();
  }
  
  
  // soft delete event listener
  protected override void DeleteEntity(NHibernate.Event.IEventSource session, object entity, ..)
  {               
      var repositoryEntity = entity as deletableentity;
      if (repositoryEntity != null)
      {
          if (!repositoryEntity.IsDeleted)
          {
              // this marks the entity as deleted
              repositoryEntity.isDeleted = true;
  
              // cascade delete
              this.CascadeBeforeDelete(session, persister, repositoryEntity, entityEntry, transientEntities);
              this.CascadeAfterDelete(session, persister, repositoryEntity, transientEntities);          
          }
      }
  }
  
  public void OnPostUpdate(PostUpdateEvent @event)
  {
      if (@event == null) throw new ArgumentNullException("event");
  
      var entity = @event.Entity as deletableentity;
  
      // Evict any entities that have been set as deleted from first level cache.
      if (entity != null &amp;&amp; entity.IsDeleted)
      {
          @event.Session.Evict(entity);
      }
  }
  </code></pre>
  
  <p>Any ideas on how to resolve it?</p>