Nhibernate后续读取存储过程调用

时间:2015-07-23 20:20:27

标签: c# nhibernate ado second-level-cache

我有一个NHibernate和ADO.NET的奇怪行为。我使用NHibernate作为标准ORM,但ADO.NET的某些操作更快(如批处理或调用存储过程)。

所以我做了以下

var item = _items.GetById(100);

然后我调用一个存储过程,该存储过程使用以下指令更新同一个实体

 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);
}

因此,使用NHibernate存储库再次检索的最后一个操作会模糊该项目。问题是该项目未随数据库更新。它似乎是以前的版本。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用class ContactsController < ApplicationController before_action :find_contact, only: [:show, :edit, :update, :destroy] def index @contacts = Contact.all.order("created_at DESC") end def new @contact = Contact.new end def create @contact = Contact.new(post_params) if @contact.save flash[:notice] = "Contact created" redirect_to(:action=>'index', :contact_id => @contact.id) else @contacts = Contacts.order() render('new') end end def edit end private def find_contact @contact=Contact.find(params[:id]) end def post_params params.require(:contact).permit(:name, :clink) end end 对象的item方法强制NHibernate刷新Refresh的缓存版本:

session
相关问题