NoSessionBindingtoCurrentContext我跟着这个教程:http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx尝试加载页面时,我没有收到“没有会话绑定到当前上下文”错误(mvc3)。publicstaticISessionFactoryBuildSessionFactory(){returnFluently.Configure().Database(MsSqlConfiguration.MsSql2008//.ConnectionString(@"Server=.SQLExpress;Database=db1;Uid=dev;Pwd=123;").ShowSql())//.ExposeConfiguration(c=>c.SetProperty("current_session_context_class","web"))//.CurrentSessionContext().Mappings(m=>m.FluentMappings.AddFromAssemblyOf()).ExposeConfiguration(cfg=>newSchemaExport(cfg).Create(false,false)).BuildSessionFactory();}实际错误在我的Repository.cs文件中:第114行:publicvirtualTGet(intid)第115行:{第116行:return_sessionFactory.GetCurrentSession()。获取(编号);第117行:}第118行:当我对此进行调试时,_sessionFactory不是null或任何东西,它似乎无法找到绑定的会话。我在我的web.config中连接了httpmodule并且它确实运行了,所以这不是问题。在我的nhibernate配置中,我尝试了两种方法:.ExposeConfiguration(c=>c.SetProperty("current_session_context_class","web"))和.CurrentSessionContext()但这没有用。听起来您没有将会话绑定到上下文。请参见以下示例:publicclassSessionFactory{protectedstaticISessionFactorysessionFactory;私有静态ILog日志=LogManager.GetLogger(typeof(SessionFactory));//为了简洁省略了几个函数publicstaticISessionGetCurrentSession(){if(!CurrentSessionContext.(GetSessionFactory()))绑定(GetSessionFactory()。OpenSession());返回GetSessionFactory()。获取当前会话();}publicstaticvoidDisposeCurrentSession(){ISessioncurrentSession=CurrentSessionContext。解除绑定(获取会话)。当前会话()关闭();当前会话.Dispose();上面的关键点是,无论何时检索第一个会话,您都将它绑定到您正在使用的任何上下文。以上就是C#学习教程:没有session绑定当前上下文共享所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
