1 App.config-configuratie:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectienaam="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); Initiële catalogus=Nhibernate; Geïntegreerde beveiliging=SSPI--> database='nhibernate'; server=.; uid='sa'; pwd='sa'; </property> <property name="adonet.batch_size">10</property> <property name="show_sql">waar</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 configuratie Configuratieconfiguratie = nieuwe configuratie(). AddAssembly("Test.Model");
fabriek = configuratie. BuildSessionFactory(); Sessie = fabriek. OpenSession(); #endregion
-------------------------------------------------------------------------------------------------------------------------- </configuration>
2 Web.config Configuratie Let op de bytepositie die geplaatst moet worden
<!--NHibernate --> <sectienaam="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> <property name="connection.connection_string">database='nhibernate'; server=.; uid='sa'; pwd='sa'; </property> <property name="show_sql">waar</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> </session-factory> </hibernate-configuration> <!--NHibernate--> <appSettings/>
---------------------------------------------------------------------------------------------------------
#region 2. Gebruik web.config om te configureren
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(). AddAssembly("Test.Model"); fabriek = configuratie. BuildSessionFactory(); Sessie = fabriek. OpenSession();
#endregion
-----------------------------------------------------------------------------------------------------------
3 NHibernate.cfg.xml configuratie:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-mapping-2.2"> <session-factory xmlns="urn:nhibernate-configuration-2.2"> <!-- eigenschappen --> <property name="connection.provider">NHibernate.Connection.DriverConnectionProviderProvider</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">waar</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <property name="use_outer_join">waar</property> <!-- mappingbestanden --> <mapping assembly="Test.Model" /> </session-factory> </hibernate-configuration>
-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------
#region 3. Maak gebruik van NHibernate.cfg.xml configuratie
NHibernate.Cfg.Configuration config = nieuwe NHibernate.Cfg.Configuration(); configuratie. Configureer(@"C:\Documenten en Instellingen\Administrator\Desktop\Tekst\NHibernateTest\WebSite1\NHibernate.cfg.xml"); fabriek = configuratie. BuildSessionFactory(); Sessie = fabriek. OpenSession(); #endregion
4 Stel een MyConfiguration.cs structuur op als volgt:
publieke statische NHibernate.Cfg.Configuration GetConfig() {
NHibernate.Cfg.Configuration config = nieuwe NHibernate.Cfg.Configuration(); configuratie. SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider"); configuratie. SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver"); configuratie. SetProperty("hibernate.connection.connection_string", "database='nhibernate'; server=.; uid='sa'; pwd='sa'; "); configuratie. SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate"); configuratie. SetProperty("hibernate.use_outer_join", "true"); configuratie. SetProperty("hibernate.show_sql", "false"); configuratie. SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"); configuratie. AddAssembly("Test.Model"); Return config;
}
------------------------------------------------------------------------------------------------------------------------------
#region 4. Gebruik MyConfiguration.cs configuratie
NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig(); Gebruik MyConfiguration.cs fabriek = configuratie. BuildSessionFactory(); factory = MyConfiguration.GetConfig(). BuildSessionFactory(); Sessie = fabriek. OpenSession(); #endregion |