解决方案 »

  1.   

    NHibernateHelper 基本上封装的问题
      

  2.   

    这是这个类
     public sealed class NHibernateHelper
        {
            private const string CurrentSessionKey = "nhibernate.current_session";
            private static readonly ISessionFactory sessionFactory;        static NHibernateHelper()
            {
                sessionFactory = new Configuration().Configure().BuildSessionFactory();
            }        public static ISession GetCurrentSession()
            {
                HttpContext context = HttpContext.Current;
                ISession currentSession = context.Items[CurrentSessionKey] as ISession;            if (currentSession == null)
                {
                    currentSession = sessionFactory.OpenSession();
                    context.Items[CurrentSessionKey] = currentSession;
                }            return currentSession;
            }        public static void CloseSession()
            {
                HttpContext context = HttpContext.Current;
                ISession currentSession = context.Items[CurrentSessionKey] as ISession;
                if (currentSession == null)
                {
                    return;
                }
                currentSession.Close();
                context.Items.Remove(CurrentSessionKey);
            }        public static void CloseSessionFactory()
            {
                if (sessionFactory != null)
                {
                    sessionFactory.Close();
                }
            }
        }
      

  3.   

      if (currentSession == null||currenSession.IsConnected==false)
                {
                    currentSession = sessionFactory.OpenSession();
                    context.Items[CurrentSessionKey] = currentSession;
                }