Deano
Deano

Reputation: 1760

Reading Hibernate Properties from Web.config

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?

Upvotes: 3

Views: 2536

Answers (2)

Chris S
Chris S

Reputation: 65436

<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.

Upvotes: 0

Sean Carpenter
Sean Carpenter

Reputation: 7721

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.

Upvotes: 4

Related Questions