我使用nhiberate,存储库模式。困惑为什么我的实体变化反映没有提交?
例如MakePersistance样子:
public T MakePersistent(T entity)
{
Session.Save(entity);
return entity;
}
在HTTP模块的开始请求:
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分析器中查看)数据库执行更新,然后检索相同的实体是新鲜/最新的。
所以我做:
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,虽然由于分贝是陈旧的,直到请求结束,并提交到数据库?