views:

87

answers:

3

I have a MVC web site that worked well, until I moved it into test, where it fell over. After some investigation If found that because I had split the 'model' into its own seperate project, the model was using the connectionstring from its own app.config (not the web.config of the website).

So I found an article somewhere (can't find the link now), showing how I can use the web.config instead - great, I got the site into test.

Unfortuanatly, the next time I tried to get the site implemented in test again, I got the same problem, and after a while discovered that somehow the app.config of my model had re-written the connection string DOH!

What is the best practice here? I need the database configuration to be in a file that is easily changed by my system administrators

+2  A: 

An ASP.NET application will never use app.config, to the best of my knowledge.

John Saunders
The problem is that the class library (my DAL) does use the app.config (rather than the web.config).
Grayson Mitchell
No, it does not. What makes you think so? In .NET, there is never more than one config file per AppDomain, unless _the_ config file redirects part of the configuration to another. Are you perhaps using Settings in the library? If so, try changing a value from the app.config and see if it matters when running from ASP.NET.
John Saunders
+1  A: 

If your project has both an App.config and a Web.config file then you'll want to tie them together so they share the connection string. Doing so is easy. In the *.config files add the line:

<connectionStrings configSource="WebCS.config" />

The webcs.config will look like:

<connectionStrings>
 <add name="YourConnString" connectionString="Data Source=Server;Initial Catalog=YourDB;User ID=SQLID;Password=Password" providerName="System.Data.SqlClient"/>
</connectionStrings>

This will permit you to reference the same connection string from multiple places and make it easy to have connection strings that differ on your dev, test and deployment servers.

Update: If you are doing TDD or are otherwise separating out your logic into a separate DLL you may have both types of .Config files and thus the issue that you raise. Of course, a separate Business Logic DLL is a "best practice" for creating forms-based Web apps in ASP.NET. For example, I place only UI code in the web site itself and then call into a DLL for all business-related and data handling logic. This not only helps with a separation of concerns but it makes testing easy as well.

Mark Brittingham
Great, this is what I was looking for.
Grayson Mitchell
Your welcome. Thanks for choosing this answer as "the answer."
Mark Brittingham