Reading hibernation properties from Web.config

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

+3
source share
2 answers

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

+4
source
<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 is referenced only once.

0
source

Source: https://habr.com/ru/post/1699136/


All Articles