• 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

  • 相关阅读:
    Android Wifi简单管理与操作
    Android 语音识别(其它资料里面的代码)
    Android 读取资源文件下面的文件
    slice,substr和substring的区别
    !important minheight
    Jquery之美中不足之三delegate的缺憾
    事件切片
    QWrap简介之:apps果实篇之:定制
    QWrap简介之:apps果实篇之:小结
    QWrap简介之:结语
  • 原文地址:https://www.cnblogs.com/luluping/p/1758561.html
Copyright © 2020-2023  润新知