現在、NHibernate と Autofac と共に ASP.NET Web API を使用しています...更新がデータベースにコミットされないという問題があります。次のように、アクションが実行されるたびに、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();
}
これは、リポジトリの追加、読み取り、および削除機能に対して正常に機能します。残念ながら、私のアップデートは機能していないようです (いくつかの方法を試しましたが):
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;
}
トランザクション中にオブジェクトを変更する場合に読んだことから、これは NHibernate によって追跡され、トランザクションがコミットされたときに実行されるため、Update または SaveOrUpdate を手動で呼び出す必要はありません。
更新機能が正しく動作しないのはなぜですか?
ありがとう!