|
|
Опубликовано 08.08.2016 13:17:51
|
|
|

1 App.config configuration:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <имя раздела="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" requirePermission="false"/> </configSections> <Hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory> <имя свойства="connection.provider">NHibernate.Connection.DriverConnectionProvider,NHibernate</property> <имя свойства="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <имя свойства="connection.connection_string"> <!--Server=(локальный); начальный каталог=nhibernate; Интегрированная безопасность=SSPI--> database='nhibernate'; сервер=.; uid='sa'; pwd='sa'; </property> <имя свойства="adonet.batch_size">10</property> <имя свойства="show_sql">true</property> <имя свойства="диалект">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 configuration Конфигурация конфигурации = новая конфигурация(). AddAssembly («Test.Model»);
Фабрика = конфигурация. BuildSessionFactory(); сессия = фабрика. OpenSession(); #endregion
-------------------------------------------------------------------------------------------------------------------------- </configuration>
2 Конфигурация Web.config Обратите внимание на расположение байта
<!--NHibernate --> <имя раздела="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/> <!--НХибернейт--> </configSections> <!--НХибернейт--> <Hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <имя свойства="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <имя свойства="диалект">NHibernate.Dialect.MsSql2005Dialect</property> <имя свойства="connection.connection_string">database='nhibernate'; сервер=.; uid='sa'; pwd='sa'; </property> <имя свойства="show_sql">true</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> </session-factory> </hibernate-configuration> <!--НХибернейт--> <appSettings/>
---------------------------------------------------------------------------------------------------------
#region 2. Используйте web.config для настройки через
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(). AddAssembly («Test.Model»); Фабрика = конфигурация. BuildSessionFactory(); сессия = фабрика. OpenSession();
#endregion
-----------------------------------------------------------------------------------------------------------
3 NHibernate.cfg.xml Конфигурация:
<?xml version="1.0" encoding="utf-8" ?> <Hibernate-configuration xmlns="urn:nhibernate-mapping-2.2"> <session-factory xmlns="urn:nhibernate-configuration-2.2"> <!-- свойства --> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <имя свойства="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <имя свойства="диалект">NHibernate.Dialect.MsSql2005Dialect</property> <имя свойства="connection.connection_string">database='nhibernate'; сервер=.; uid='sa'; pwd='sa'; </property> <имя свойства="show_sql">true</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <имя свойства="use_outer_join">true</property> <!-- файлы отображения --> <mapping assembly="Test.Model" /> </session-factory> </hibernate-configuration>
-------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------
#region 3. Используйте NHibernate.cfg.xml конфигурацию
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(); Конфигурация. Configure(@"C:\Documents and Settings\Administrator\Desktop\Text\NHibernateTest\WebSite1\NHibernate.cfg.xml"); Фабрика = конфигурация. BuildSessionFactory(); сессия = фабрика. OpenSession(); #endregion
4 Создайте структуру MyConfiguration.cs следующим образом:
public static NHibernate.Cfg.Configuration GetConfig() {
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration(); Конфигурация. SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider"); Конфигурация. SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver"); Конфигурация. SetProperty("hibernate.connection.connection_string", "database='nhibernate'; сервер=.; uid='sa'; pwd='sa'; "); Конфигурация. SetProperty ("hibernate.dialect", "NHibernate.Dialect.MsSql2005Dialect,NHibernate"); Конфигурация. SetProperty("hibernate.use_outer_join", "true"); Конфигурация. SetProperty("hibernate.show_sql", "ложный"); Конфигурация. SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"); Конфигурация. AddAssembly («Test.Model»); конфигурацию возврата;
}
------------------------------------------------------------------------------------------------------------------------------
#region 4. Используйте MyConfiguration.cs конфигурацию
NHibernate.Cfg.Configuration config = MyConfiguration.GetConfig(); Используйте MyConfiguration.cs Фабрика = конфигурация. BuildSessionFactory(); factory = MyConfiguration.GetConfig(). BuildSessionFactory(); сессия = фабрика. OpenSession(); #endregion |
Предыдущий:webapi:ObjectContent1类型未能序列化内容类型“application/xml; charset=ut...Следующий:Не могу прочитать раздел конфигурации «XXX», потому что там отсутствует оператор раздела
|