[英]Why isn't my NHibernate update working?
I'm currently using the ASP.NET Web Api along with NHibernate & Autofac...I'm having an issue where my update is not being committed to the database. 我目前正在将NHibernate和Autofac与ASP.NET Web Api一起使用...我遇到的问题是我的更新未提交到数据库。 I'm using an ActionFilterAttribute to open and close a transcation every time an action is performed like so:
每次执行操作时,我都使用ActionFilterAttribute打开和关闭事务,如下所示:
private ISessionFactory SessionFactory { get; set; }
public TransactionAttribute()
{
SessionFactory = WebApiApplication.SessionFactory;
}
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
session.BeginTransaction();
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var session = SessionFactory.GetCurrentSession();
var transcation = session.Transaction;
if (transcation != null && transcation.IsActive)
{
transcation.Commit();
}
session = CurrentSessionContext.Unbind(SessionFactory);
session.Close();
}
Which works fine for the add, read, and delete functions in my repository. 对于我的存储库中的添加,读取和删除功能,该方法很好用。 Unfortunately, my update doesn't seem to be working (though I've tried it several ways):
不幸的是,我的更新似乎没有用(尽管我已经尝试了几种方法):
public bool Update(Client client)
{
var result = Get(client.ClientID);
if (result == null)
{
return false;
}
result.Name = client.Name;
result.Acronym = client.Acronym;
result.Website = client.Website;
return true;
}
From what I've read if modifying an object during a transaction, there's no need to manually call Update or SaveOrUpdate, as this is tracked by NHibernate & performed when the transaction is committed. 根据我在事务处理过程中修改对象所获得的信息,无需手动调用Update或SaveOrUpdate,因为NHibernate会跟踪该操作或在事务提交时执行此操作。
Why would my update function not be working properly? 为什么我的更新功能无法正常工作?
Thanks! 谢谢!
I figured this out on my own--basically I was creating 2 sessions (one in my TranscationAttribute and one due to DI into my repository). 我自己弄清楚了这一点-基本上我正在创建2个会话(一个在我的TranscationAttribute中,另一个是由于DI进入我的存储库)。 The reason it was creating two is pretty explicit in the OnActionExecuting statement...I'm not checking to see whether any session factory is currently bound to the CurrentSessionContext.
它创建两个对象的原因在OnActionExecuting语句中非常明确...我没有检查是否当前有任何会话工厂绑定到CurrentSessionContext。 This is the new code I'm using:
这是我正在使用的新代码:
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!CurrentSessionContext.HasBind(SessionFactory))
{
CurrentSessionContext.Bind(SessionFactory.OpenSession());
}
var session = SessionFactory.GetCurrentSession();
session.BeginTransaction();
}
The issue I had after implementing this code is that the logic is not sound..when the session is DI'ed into my repository ISession, it was not being automatically bound. 实现此代码后,我遇到的问题是逻辑不健全。当将会话DI放入我的存储库ISession中时,它没有被自动绑定。 My solution was to bind it in the constructor of my repository like so:
我的解决方案是将其绑定到存储库的构造函数中,如下所示:
public ClientRepository(ISession session)
{
if (session == null) throw new ArgumentNullException("nhSession");
this._session = session;
CurrentSessionContext.Bind(_session);
}
But I'm not 100% sure that this is going to be safe with many simultaneous HTTP requests...going to bring this question over to code review unless someone has an answer for me here! 但是我不是100%肯定会同时进行许多HTTP请求是安全的...除非有人在这里为我提供答案,否则请将此问题提交代码审查!
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.