This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17312|Reply: 0

[Source] There are three configuration methods for Nhibernate in .net

[Copy link]
Posted on 8/8/2016 1:17:51 PM | | |

1 App.config configuration:

<?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
            Configuration config = new Configuration(). AddAssembly("Test.Model");        

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

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

2 Web.config Configuration Note the byte position to be placed

<!--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. Use web.config to configure through

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

        #endregion

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

3 NHibernate.cfg.xml Configuration:

<?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. Utilize NHibernate.cfg.xml configuration

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



4 Create a MyConfiguration.cs structure as follows:



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. Use MyConfiguration.cs configuration

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




Previous:webapi:ObjectContent1类型未能序列化内容类型“application/xml; charset=ut...
Next:Cannot read the configuration section "XXX" because it is missing the section statement
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com