是的,所以我可以很容易地创建项目,而无需在 Session.SaveOrUpdate(entity) 之后显式调用 Session.Flush() - 但更新或删除时并非如此。除了键入 Session.Flush() 我还可以键入 Session.Transaction.Commit() 来实现相同的效果(即即时删除/更新)。
这让我相信我的 NHibernateActionFilter 事务处理存在问题,即它只提交了一些时间而不是所有时间。我不太确定为什么?
品牌控制器:会话控制器
[HttpPost]
public ActionResult Edit (EditBrandViewModel editBrandViewModel)
{
if(ModelState.IsValid)
{
var model = editBrandViewModel;
var brand = Session.Get<Brand>(model.Id);
Mapper.Map(model, brand);
Session.SaveOrUpdate(brand);
Session.Flush();
return RedirectToAction("Details/" + model.Id);
}
return View(editBrandViewModel);
}
public ActionResult Delete(int id)
{
var brand = Session.Get<Brand>(id);
Session.Delete(brand);
Session.Flush();
return RedirectToAction("Index");
}
会话控制器:
public class SessionController : Controller
{
public HttpSessionStateBase HttpSession
{
get { return base.Session; }
}
public new ISession Session { get; set; }
}
NHibernateActionFilter:
public class NHibernateActionFilter : ActionFilterAttribute
{
private static ISessionFactory sessionFactory;
public NHibernateActionFilter(ISessionFactory sessionFactory)
{
sessionFactory = sessionFactory;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var sessionController = filterContext.Controller as SessionController;
if (sessionController == null)
return;
sessionController.Session = sessionFactory.OpenSession();
sessionController.Session.BeginTransaction();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var sessionController = filterContext.Controller as SessionController;
if (sessionController == null)
return;
using (var session = sessionController.Session)
{
if (session == null)
return;
if (!session.Transaction.IsActive)
return;
if (filterContext.Exception != null)
session.Transaction.Rollback();
else
session.Transaction.Commit();
}
}
}