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
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:
The webcs.config will look like:
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.