如何将应用程序设置加载到 NHibernate.Cfg.Configuration 对象?

发布于 2024-08-16 16:08:57 字数 111 浏览 9 评论 0原文

如何使用 App.config 中的 System.Configuration.ConfigurationManager 将应用程序设置加载到 NHibernate.Cfg.Configuration 对象?

How to load application settings to NHibernate.Cfg.Configuration object by using System.Configuration.ConfigurationManager from App.config?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-08-23 16:08:57

hibernate配置也可以移到app.config中,这简化了启动代码。请参阅 NHibernate 参考手册中的 XML 配置文件 部分。

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>

The hibernate configuration can also be moved into app.config, which simplifies the startup code. See section XML Configuration File in the NHibernate reference manual.

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

And in 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>
明月松间行 2024-08-23 16:08:57

应用程序配置:

<?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();

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# code:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文