1 App.config-konfiguration:
<?xml version="1.0" kodning="utf-8" ?> <configuration> <configSections> <sektionsnamn="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> <egendomsnamn="connection.connection_string"> <!--Server=(lokal); Initial katalog=nhibernate; Integrerad säkerhet=SSPI--> databas='nhibernate'; server=.; uid='sa'; pwd='sa'; </property> <egendomsnamn="adonet.batch_size">10</property> <egendomsnamn="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 konfiguration Konfigurationskonfiguration = ny konfiguration(). AddAssembly("Test.Model");
fabrik = konfiguration. BuildSessionFactory(); Session = fabrik. OpenSession(); #endregion
-------------------------------------------------------------------------------------------------------------------------- </configuration>
2 Web.config-konfiguration Notera bytepositionen som ska placeras
<!--NHibernate --> <sektionsnamn="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.DriverConnectionProviderProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <egendomsnamn="connection.connection_string">databas='nhibernate'; server=.; uid='sa'; pwd='sa'; </property> <egendomsnamn="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. Använd web.config för att konfigurera
NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration(). AddAssembly("Test.Model"); fabrik = konfiguration. BuildSessionFactory(); Session = fabrik. OpenSession();
#endregion
-----------------------------------------------------------------------------------------------------------
3 NHibernate.cfg.xml Konfiguration:
<?xml version="1.0" kodning="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-mapping-2.2"> <session-factory xmlns="urn:nhibernate-configuration-2.2"> <!-- egenskaper --> <property name="connection.provider">NHibernate.Connection.DriverConnectionProviderProvider</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <egendomsnamn="connection.connection_string">databas='nhibernate'; server=.; uid='sa'; pwd='sa'; </property> <egendomsnamn="show_sql">true</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <egendomsnamn="use_outer_join">true</property> <!-- mappfiler --> <mapping assembly="Test.Model" /> </session-factory> </hibernate-configuration>
-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------
#region 3. Använd NHibernate.cfg.xml konfiguration
NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration(); konfiguration. Konfigure(@"C:\Documents and Settings\Administrator\Desktop\Text\NHibernateTest\WebSite1\NHibernate.cfg.xml"); fabrik = konfiguration. BuildSessionFactory(); Session = fabrik. OpenSession(); #endregion
4 Skapa en MyConfiguration.cs struktur enligt följande:
public static NHibernate.Cfg.Configuration GetConfig() {
NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration(); konfiguration. SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider"); konfiguration. SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver"); konfiguration. SetProperty("hibernate.connection.connection_string", "database='nhibernate'; server=.; uid='sa'; pwd='sa'; "); konfiguration. SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate"); konfiguration. SetProperty("hibernate.use_outer_join", "true"); konfiguration. SetProperty("hibernate.show_sql", "false"); konfiguration. SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"); konfiguration. AddAssembly("Test.Model"); returnera konfiguration;
}
------------------------------------------------------------------------------------------------------------------------------
#region 4. Använd MyConfiguration.cs konfiguration
NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig(); Använd MyConfiguration.cs fabrik = konfiguration. BuildSessionFactory(); factory = MyConfiguration.GetConfig(). BuildSessionFactory(); Session = fabrik. OpenSession(); #endregion |