zoukankan      html  css  js  c++  java
  • nhibernate使用记录

    最基本的元素NHibernate.dll(这是必须的,各种对数据库操作都需要它),NHibernate.ByteCode.LinFu和NHibernate.ByteCode.Castle这两个,任选其一,用作数据持久化,hibernate.cfg.xml,这个是sessionfactory 如何操作数据库的一个配置,也可以放在web.config里,但是单拿出来更好,这些是基本配置。困了,下一回记录剩下的东西。然后就是实体类了和实体类的映射文件。最后是执行代码了。

      public 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;
            }

        }

    我把它的初始化放在global下,其实放在一个类里边也是一样的。

     Models.NHibernateHelper nhHelper=new MvcApplication2.Models.NHibernateHelper();

    下回记录多表操作

  • 相关阅读:
    [BZOJ3751] [NOIP2014] 解方程 (数学)
    [BZOJ4198] [Noi2015] 荷马史诗 (贪心)
    [BZOJ4034] [HAOI2015] T2 (树链剖分)
    [BZOJ1880] [Sdoi2009] Elaxia的路线 (SPFA & 拓扑排序)
    [BZOJ1088] [SCOI2005] 扫雷Mine
    [BZOJ1004] [HNOI2008] Cards (Polya定理)
    [BZOJ1009] [HNOI2008] GT考试 (KMP & dp & 矩阵乘法)
    [BZOJ1503] [NOI2004] 郁闷的出纳员 (treap)
    [BZOJ1059] [ZJOI2007] 矩阵游戏 (二分图匹配)
    BZOJ2626: JZPFAR
  • 原文地址:https://www.cnblogs.com/chenleinet/p/1800301.html
Copyright © 2011-2022 走看看