-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: share connection string from WebApp web.config
PostPosted: Tue Jun 02, 2009 10:56 am 
Newbie

Joined: Tue Jun 02, 2009 10:42 am
Posts: 2
I need to pass the DB ConnectionString configuration from my WebApp's web.config to the NHibernate config section in a second web.config file.

In my web app my connection looks like this:
<add name="SiteSqlServer" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Test05.mdf;" providerName="System.Data.SqlClient" />

And i'm setting the same connection again in a second web.config file for a service:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
....
.....
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=C:\MyDocuments\Projects\Test\App_Data\Test05.mdf; </property>
....
....
</session-factory>
</hibernate-configuration>

I've already tried to copy all the hibernate-configuration code in the first web.config but apparently it doesn't recognize the configuration, i'm getting an error when i do this.
The thing is that i need to have the database connection in just one web.config file not in both files, is this possible ?
Thanks in advance for any help...


Top
 Profile  
 
 Post subject: Re: share connection string from WebApp web.config
PostPosted: Tue Jun 02, 2009 11:27 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try

<property name="connection.connection_string_name">SiteSqlServer</property>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: share connection string from WebApp web.config
PostPosted: Tue Jun 02, 2009 11:52 am 
Newbie

Joined: Tue Jun 02, 2009 10:42 am
Posts: 2
thanks for replying so fast, i've already try your advice but i'm getting the same error, maybe the fact that the second web.config is in a WCFService project has something to do with ?
In my first web config i got the connectionString, and i want to use that connection in the second webconfig without declaring again.

First web.config (Main WebApp Project)
<connectionStrings>
<add name="SiteSqlServer" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=C:\MyDocuments\Projects\Test\App_Data\Test05.mdf;" providerName="System.Data.SqlClient" />
</connectionStrings>


Second web.config (ServiceProject)

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string_name">SiteSqlServer</property>
</session-factory>
</hibernate-configuration>

This one above doesn't work. but if i add the same <connectionString> tag like in the First web.config it works fine but that is what i want to avoid. The two web.config files are in separate projects.


Top
 Profile  
 
 Post subject: Re: share connection string from WebApp web.config
PostPosted: Tue Jun 02, 2009 2:24 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try an manualle set the config file:

ISessionFactory sf = new Configuration()
.Configure("/path/to/config.cfg.xml")
.BuildSessionFactory();

or you can manually set the property in your code:

cfg.SetProperty("connection.connection_string", get_connection_string_from_other_web_config);
cfg.BuildSessionFactory();

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.