从Web.config读取Hibernate属性

时间:2022-07-30 11:26:59

The C# project I'm working on uses nHibernate and the connection string is in the web.config as a property of a Hibernate element. I need to read the connection string in the installer to get a connection manually without using Hibernate. I know I can use configManager.connectionStrings, but as the connection string is already defined in the Hibernate portion of web.config I don't want to copy it again into the connectionStrings element. So how can I access this?

我正在使用的C#项目使用nHibernate,连接字符串在web.config中作为Hibernate元素的属性。我需要在安装程序中读取连接字符串以手动获取连接而不使用Hibernate。我知道我可以使用configManager.connectionStrings,但由于连接字符串已经在web.config的Hibernate部分中定义,我不想再将它复制到connectionStrings元素中。那我该如何访问呢?

2 个解决方案

#1


4  

You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.

您可以将连接字符串放在web.config的 部分中,然后让NHibernate从那里获取它。在NHibernate设置中,删除 属性并将其替换为 ,从 部分提供名称。详情请见此处。

#2


0  

<hibernate>
    <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
    <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
    <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
    <add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>

<connectionStrings>
    <add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>

This makes it available in your ConfigurationManager, but only referenced once.

这使它在ConfigurationManager中可用,但仅引用一次。

#1


4  

You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.

您可以将连接字符串放在web.config的 部分中,然后让NHibernate从那里获取它。在NHibernate设置中,删除 属性并将其替换为 ,从 部分提供名称。详情请见此处。

#2


0  

<hibernate>
    <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
    <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
    <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
    <add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>

<connectionStrings>
    <add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>

This makes it available in your ConfigurationManager, but only referenced once.

这使它在ConfigurationManager中可用,但仅引用一次。