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