简体   繁体   English

存储过程调用后,NHibernate随后读取

[英]Nhibernate subsequent read after stored procedure call

I have a strange behaviour of NHibernate and ADO.NET. 我有NHibernate和ADO.NET的奇怪行为。 I'm using NHibernate as standard ORM but some operation are faster with ADO.NET (like batch or calling a stored procedure). 我使用NHibernate作为标准ORM,但是使用ADO.NET的某些操作更快(例如批处理或调用存储过程)。

So I'm doing the following 所以我正在做以下

var item = _items.GetById(100);

Then I do call a stored procedure that do update the same entity with the following instructions 然后,我确实调用了一个存储过程,该存储过程确实按照以下说明更新了同一实体

 var session = _sessionManager.OpenSession();
 using (var tx = session.BeginTransaction())
 {
      var item = _items.GetAll().FirstOrDefault(x => x.ItemCode == code);

      var cmd = session.Connection.CreateCommand();
      session.Transaction.Enlist(cmd);
      cmd.CommandType = CommandType.StoredProcedure;

      SqlParameter id = SQLUtil.GetParam("@bigId", SqlDbType.BigInt, inout: false);

      cmd.CommandText = "storedUpdateItem";
      cmd.Parameters.Add(SQLUtil.GetParam("@tstTS", SqlDbType.Timestamp, inout: false));
      cmd.Parameters.Add(id);

      int ret = cmd.ExecuteNonQuery();
      if (ret != 1)
      {
          throw new ApplicationException("Something gone wrong with update");
      } 
      tx.Commit();

      return _items.GetById(id.Value);
}

So the last operation to retrieve again using a NHibernate repository patter the item. 因此,使用NHibernate存储库再次进行检索的最后一个操作会对该项目进行分类。 The problem is that the item is not update with the database. 问题在于该项目未随数据库更新。 It seems to be the previous version. 好像是以前的版本。 Any idea? 任何想法?

You can force NHibernate to refresh the cached version of item by using the Refresh method of the session object: 您可以强制NHibernate的刷新的缓存版本item通过Refresh的方法session对象:

      ...
      var item = _items.GetById(id.Value);
      session.Refresh(item);
      return item;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM