No session bound to the current context

后端 未结 1 533
孤独总比滥情好
孤独总比滥情好 2021-01-02 07:32

I followed this tutorial: http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx

I am not getting a \'no se

相关标签:
1条回答
  • 2021-01-02 08:14

    It sounds like you are not binding your session to the context. Look at the below for an example:

    public class SessionFactory
    {
        protected static ISessionFactory sessionFactory;
        private static ILog log = LogManager.GetLogger(typeof(SessionFactory));
    
        //Several functions omitted for brevity
    
        public static ISession GetCurrentSession()
        {
            if(!CurrentSessionContext.HasBind(GetSessionFactory()))
                CurrentSessionContext.Bind(GetSessionFactory().OpenSession());
    
            return GetSessionFactory().GetCurrentSession();
        }
    
        public static void DisposeCurrentSession()
        {
            ISession currentSession = CurrentSessionContext.Unbind(GetSessionFactory());
    
            currentSession.Close();
            currentSession.Dispose();
        }
    }
    

    The key to the above is that whenever you retrieve your first session you bind it to whatever context you are using.

    0 讨论(0)
提交回复
热议问题