我正在使用Ninject,流利的NHibernate和ASP.NET MVC。为什么NHibernate不跟踪对我的实体所做的更改?
到目前为止,一切工作正常,我没有得到任何错误,我能够从存储库中查询出很好,但我无法提交任何更改。
我控制器的方法是这样的
[HttpPost]
[UnitOfWork]
public ActionResult Method(int id)
{
// Lookup entity, toggle a bool property on it and that is it
}
我的UnitOfWork属性看起来像这样
public class UnitOfWorkAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
NHibernateSession.Current.BeginTransaction();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Exception == null && NHibernateSession.Current.Transaction.IsActive)
{
NHibernateSession.Current.Transaction.Commit();
}
}
}
这两种方法被调用,没有错误引发的问题是,当NHibernateSession.Current
(这只是返回NHibernate.ISession
)被调用,ISession.IsDirty()
为假。 NHibernate不认为有任何改变。
我以前在其他项目中使用过类似的设置,没有问题,唯一不同的是我换出了Ninject的StructureMap,我不太熟悉它。
相关的绑定是
Bind<IEntityRepository>().To<EntityRepository>().InRequestScope();
Bind<ISessionFactory>().ToMethod(x => NHibernateSession.CreateSessionFactory()).InSingletonScope();
Bind<ISession>().ToMethod(x => x.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
任何想法我做了什么错?我猜这跟我在搞会话处理上有点关系,但我不确定究竟是什么。
编辑:这是当前调用返回的内容。应该存储会话,以便每次都不必创建新会话。
public static ISession Current
{
get
{
var session = GetExistingSession();
if (session != null)
return session;
session = _sessionFactory.OpenSession();
HttpContext.Current.Items[SessionKey] = session;
return session;
}
}
你如何注入ISession到NHibernateSession.Current? – dotjoe 2012-02-15 21:54:05
它没有真正注入。一个被调用的方法将构建一个SessionFactory并将其存储在我的NHibernateSession类的一个字段中。所以'.Current'会在需要时调用'SessionFactory.OpenSession'。 – Brandon 2012-02-15 21:57:29