Nhibernate three ways to configure

Source: Internet
Author: User

1 configuration of the App. config:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "hibernate-configuration" type= "Nhibernate.cfg.configurationsectionhandler,nhibernate" Requirepermission= "false"/>
</configSections>
<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 ';p wd= ' 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>
--------------------------------------------------------------------------------------------------------------- -------

#region 2. App. Config configuration
Configuration config = new configuration (). addassembly ("Test.model");

Factory = config. Buildsessionfactory ();
Session = Factory. Opensession ();
#endregion

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

2 configuration of the Web. config Note the location of bytes placed

<!--NHibernate--
<section name= "hibernate-configuration" type= "NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
<!--nhibernate-->
</configSections>
<!--nhibernate-->
<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 ';p wd= ' sa ';</property>
<property name= "Show_sql" >true</property>
<property name= "Proxyfactory.factory_class" >nhibernate.bytecode.linfu.proxyfactoryfactory, Nhibernate.bytecode.linfu</property>
</session-factory>
<!--nhibernate-->
<appSettings/>

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

#region 2, use the Web. config configuration to pass

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"?>
<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 ';p wd= ' 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>

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

#region 3, using 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 ';p wd= ' 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

Nhibernate three ways to configure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.