我按照这个教程进行操作:http://nhforge.org/blogs/nhibernate/archive/2011/03/03/ effective-nhibernate-session-management-for-web-apps.aspx
尝试加载页面(mvc 3)时,我没有收到“没有会话绑定到当前上下文”错误。
public static ISessionFactory BuildSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008 //
.ConnectionString(@"Server=.\SQLExpress;Database=db1;Uid=dev;Pwd=123;")
.ShowSql())
//.ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
//.CurrentSessionContext<CallSessionContext>()
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<User>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
}
实际错误在我的 Repository.cs 文件中:
第 114 行:public virtual T Get(int id)
第 115 行:{
第 116 行: return _sessionFactory.GetCurrentSession().Get(id);
第 117 行:}
118号线:
当我调试它时, _sessionFactory 不为空或任何东西,它似乎找不到绑定的会话。
我在 web.config 中连接了 httpmodule,并且它确实运行了,所以这不是问题。
在我的 nhibernate 配置中,我尝试了以下两种方法:
.ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
and
.CurrentSessionContext<CallSessionContext>()
但这没有用。