|
|
Opslået på 08/08/2016 13.17.51
|
|
|

1 App.config-konfiguration:
<?xml version="1.0" kodning="utf-8" ?> <configuration> <configSections> <sektionsnavn="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" kræverTilladelse=""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=(lokal); initialt katalog=nhibernat; Integreret sikkerhed=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 konfiguration Konfigurationskonfiguration = ny Konfiguration(). AddAssembly("Test.Model");
fabrik = konfiguration. BuildSessionFactory(); Session = fabrik. OpenSession(); #endregion
-------------------------------------------------------------------------------------------------------------------------- </configuration>
2 Web.config Konfiguration Bemærk bytepositionen, der skal placeres
<!--NHibernate --> <sektionsnavn="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">true</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> </session-factory> </hibernate-configuration> <!--NHibernate--> <appSettings/>
---------------------------------------------------------------------------------------------------------
#region 2. Brug web.config til at konfigurere igennem
NHibernate.Cfg.Configuration config = new 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"> <!-- egenskaber --> <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">true</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <property name="use_outer_join">true</property> <!-- mapping-filer --> <mapping assembly="Test.Model" /> </session-factory> </hibernate-configuration>
-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------
#region 3. Udnyt NHibernate.cfg.xml konfiguration
NHibernate.Cfg.Configuration config = ny NHibernate.Cfg.Configuration(); konfiguration. Konfigurér(@"C:\Dokumenter og indstillinger\Administrator\Skrivebord\Tekst\NHibernateTest\WebSite1\NHibernate.cfg.xml"); fabrik = konfiguration. BuildSessionFactory(); Session = fabrik. OpenSession(); #endregion
4 Skab en MyConfiguration.cs struktur som følger:
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"); returner konfiguration;
}
------------------------------------------------------------------------------------------------------------------------------
#region 4. Brug MyConfiguration.cs konfiguration
NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig(); Brug MyConfiguration.cs fabrik = konfiguration. BuildSessionFactory(); factory = MyConfiguration.GetConfig(). BuildSessionFactory(); Session = fabrik. OpenSession(); #endregion |
Tidligere:webapi:ObjectContent1类型未能序列化内容类型“application/xml; charset=ut...Næste:Kan ikke læse konfigurationssektionen "XXX", fordi den mangler sektionssætningen
|