Using ConnectionString section in NHibernate

Finally NHibernate (1.2+) is able to take advantage of .NET2.0's connectionStrings section, which previously I'd have to either declare it twice (in either config file or the dedicated hibernate.cfg.xml) All you need to do is, instead of using hibernate.connection.connection_string, use the "newer" hibernate.connection.connection_string_name property.

<connectionStrings>

  <add name="myConnectionSTring" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True"

   providerName="System.Data.SqlClient" />

 </connectionStrings>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <!--<mapping assembly="WebApplication1" />-->
            <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
            <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
            <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
            <property name="connection.connection_string_name">myConnectionString</property>
            <!-- HBM Mapping Files -->
        </session-factory>
    </hibernate-configuration>


UPDATE(Thanks to Ayende):Castle's ActiveRecord(RC1/trunk) which uses NHibernate, gives you two option of taking advantage of the connectionStrings sections either by(older one)

<add key="hibernate.connection.connection_string" value="ConnectionString = ${myConnectionString}" />


or the "newer one" which takes advantage of the NH1.2+ support for the connectionStrings sections:

<add key="hibernate.connection.connection_string_name" value="myConnectionString" />

It now makes it more manageable, no need to have separate connectionString sections, more importantly though, is that you can take advantage of connectionString/protectedSections of .NET 2.0.






Published Sunday, April 08, 2007 8:12 PM by bonskijr
Filed under: ,

Comments

# re: Using ConnectionString section in NHibernate

Just to note,

Active Record can use connection_string_name just like NHibernate.

Monday, April 09, 2007 2:44 AM by Ayende Rahien