Nhibernate 困惑为什么我的实体更改反映为不提交?

Nhibernate 困惑为什么我的实体更改反映为不提交?,nhibernate,Nhibernate,我使用的是nhiberate,带有存储库模式 e、 g.makepersistence看起来像: public T MakePersistent(T entity) { Session.Save(entity); return entity; } 在httpmodule中,开始请求: ISession session = NHibernateHelper.OpenSession(); session.Begi

我使用的是nhiberate,带有存储库模式

e、 g.makepersistence看起来像:

 public T MakePersistent(T entity)
        {
            Session.Save(entity);

            return entity;
        }
在httpmodule中,开始请求:

ISession session = NHibernateHelper.OpenSession();
 session.BeginTransaction();

 CurrentSessionContext.Bind(session);
结束请求:

ISession session = CurrentSessionContext.Unbind(
            NHibernateHelper.SessionFactory);

            if (session != null)
                try
                {
                    session.Transaction.Commit();
                }
                catch (Exception ex)
                {
                    session.Transaction.Rollback();
                    //Server.Transfer("...", true);
                }
                finally
                {
                    session.Close();
                }
因此,在每个页面请求上,事务开始和结束

据我所知,这意味着如果我更新一个实体,然后在更新后查询该实体,查询将返回该实体的原始状态,因为更新尚未提交到数据库

但是,我测试(并在sql profiler中查看)db执行更新,然后检索同一实体是最新的

所以我做了:

Entity e = EntityDao.GetById(1);

// e.count outputs 0

e.Count += 1;

// e.count outputs 1 as expected

EntityDao.MakePersistant(entity);

entity = EntityDao.GetById(1);  // getting from the db again

// e.count ouputs 1  ***

但它不应该是0吗?因为在请求结束并提交到db之前,db是过时的???

如果实体的主键是Identity列,这是正常行为,因为NHibernate必须将数据持久化到数据库中,以便获取实体的ID,以便将其放置在会话中。这是所有插入后生成器的情况。 如果您不希望发生这种情况,也希望获得许多其他好处,那么您应该选择使用ORM样式的生成器之一,如HiLo/序列HiLo生成器

有关更多信息,请查看此链接:


如果实体的主键为Identity列,这是正常行为,因为NHibernate必须将数据持久化到数据库中,以便获取实体的ID,以便将其放置在会话中。这是所有插入后生成器的情况。 如果您不希望发生这种情况,也希望获得许多其他好处,那么您应该选择使用ORM样式的生成器之一,如HiLo/序列HiLo生成器

有关更多信息,请查看此链接: