Как загрузить параметры настройки приложения в NHibernate. Cfg. Объект конфигурации?

Как загрузить параметры настройки приложения в NHibernate.Cfg.Configuration объект при помощи System.Configuration.ConfigurationManager от App.config?

11
задан user366312 31 December 2009 в 11:31
поделиться

2 ответа

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Northwind" connectionString=
       "Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=True;>
  </connectionStrings>
</configuration>

C# код:

string connectionString =  System.Configuration.ConfigurationManager
                                 .ConnectionStrings["Northwind"].ToString();

NHibernate.Cfg.Configuration nHibernateConfiguration =
                                      new NHibernate.Cfg.Configuration();
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.ProxyFactoryFactoryClass,
  typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedName);
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.Dialect,
  typeof(NHibernate.Dialect.MsSql2005Dialect).AssemblyQualifiedName);
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.ConnectionString, connectionString);
nHibernateConfiguration.SetProperty(
  NHibernate.Cfg.Environment.FormatSql, "true");
nHibernateConfiguration.AddAssembly(Assembly.GetCallingAssembly());

ISessionFactory oneISessionFactory = nHibernateConfiguration
                                        .BuildSessionFactory();
20
ответ дан 3 December 2019 в 01:13
поделиться

Конфигурацию гибернации также можно переместить в app.config, что упрощает код запуска. См. Раздел Файл конфигурации XML в справочном руководстве NHibernate.

Configuration cfg = new NHibernate.Cfg.Configuration();
ISessionFactory sf = cfg.Configure().BuildSessionFactory();

И в app.config:

<configuration>
        <configSections>
            <section name="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>
                <property name="connection.dialect">NHibernate.Dialect.MsSql2005Dialect</property>
                <property name="connection.connection_string_name">Northwind</property>
                <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
                <mapping assembly="assemblyname" />
            </session-factory>
        </hibernate-configuration>
        <connectionStrings>
                <add name="Northwind" connectionString="Data Source=(local);Initial Catalog=Northwind;Trusted_Connection=True;>
        </connectionStrings>
</configuration>
24
ответ дан 3 December 2019 в 01:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: