zoukankan      html  css  js  c++  java
  • Nhibernate 三种配置方式

    1 App.config 的配置:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" requirePermission="false"/>
    </configSections>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
        <session-factory>
          <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider,NHibernate</property>
          <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
          <property name="connection.connection_string">
            <!--Server=(local);initial catalog=nhibernate;Integrated Security=SSPI-->
            database='nhibernate';server=.;uid='sa';pwd='sa';
          </property>
          <property name="adonet.batch_size">10</property>
          <property name="show_sql">true</property>
          <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
          <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
        </session-factory>
    </hibernate-configuration>
    ----------------------------------------------------------------------------------------------------------------------

    #region 2 。 App.config 配置
                Configuration config = new Configuration().AddAssembly("Test.Model");        

            factory = config.BuildSessionFactory();
              session = factory.OpenSession();
    #endregion

    --------------------------------------------------------------------------------------------------------------------------
    </configuration>

    2 web.config的配置 注意 所放字节位置

    <!--NHibernate -->
       <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
       <!--NHibernate-->
    </configSections>
    <!--NHibernate-->
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
       <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="connection.connection_string">database='nhibernate';server=.;uid='sa';pwd='sa';</property>
        <property name="show_sql">true</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
       </session-factory>
    </hibernate-configuration>
    <!--NHibernate-->
    <appSettings/>

    ---------------------------------------------------------------------------------------------------------

    #region 2、利用web.config 配置 通过

            //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration().AddAssembly("Test.Model");
            //factory = config.BuildSessionFactory();
            //session = factory.OpenSession();

            #endregion

    -----------------------------------------------------------------------------------------------------------

    3 NHibernate.cfg.xml 配置:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-configuration xmlns="urn:nhibernate-mapping-2.2">
    <session-factory xmlns="urn:nhibernate-configuration-2.2">
        <!-- properties -->
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="connection.connection_string">database='nhibernate';server=.;uid='sa';pwd='sa';</property>
        <property name="show_sql">true</property>
         <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
        <property name="use_outer_join">true</property>
        <!-- mapping files -->
        <mapping assembly="Test.Model" />
    </session-factory>
    </hibernate-configuration>

    -------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------

       #region 3、利用NHibernate.cfg.xml配置

            //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            //config.Configure(@"C:\Documents and Settings\Administrator\桌面\Text\NHibernateTest\WebSite1\NHibernate.cfg.xml");
            //factory = config.BuildSessionFactory();
            //session = factory.OpenSession();
            #endregion

    4     创建一个MyConfiguration.cs 结构如下:

    public static NHibernate.Cfg.Configuration GetConfig()
        {

          
            NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            config.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            config.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
            config.SetProperty("hibernate.connection.connection_string", "database='nhibernate';server=.;uid='sa';pwd='sa';");
            config.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate");
            config.SetProperty("hibernate.use_outer_join", "true");
            config.SetProperty("hibernate.show_sql", "false");
            config.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
            config.AddAssembly("Test.Model");
            return config;
         
        }

    ------------------------------------------------------------------------------------------------------------------------------

    #region 4、使用MyConfiguration.cs配置

            //NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig();//使用MyConfiguration.cs
            //factory = config.BuildSessionFactory();
            factory = MyConfiguration.GetConfig().BuildSessionFactory();
            session = factory.OpenSession();
            #endregion

  • 相关阅读:
    使用数据库时注意单引号、双引号和反引号的区别
    Mysql中的事件
    C#使用WindowsMediaPlayer实现视频播放
    Chart控件,鼠标选择区域,可以局部放大缩小
    C#自定义控件在添加引用后不显示在工具箱的解决方法(转)
    C# 中的#if、#elif、#else、#endif等条件编译符号
    若有派生类,则基类中的析构函数要用虚函数
    迭代器失效
    this指针
    VS调试快捷键
  • 原文地址:https://www.cnblogs.com/luluping/p/1758561.html
Copyright © 2011-2022 走看看