I have a web application that comprises the following:
When I build and deploy, there is no settings file or app.config in the Bin directory with the data access .dll, but changing the connection string in the web.config file doesn't change the database accordingly - so the connection string must be compiled into the data access dll.
What I need is one config file for my entire deployment - website, data access dlls, everything - that has one connection string which gets used. At the moment there appear to be multiple connection strings getting used or hardcoded all over the place.
How do I best resolve this mess?
Thanks for any help.
I've never had a problem with the Data Access Layer (DAL) being able to use the connection strings from my web.config
file. Usually I just copy the connection strings section from the DAL and paste it into the web.config
. I'm using the DBML designer to create the data context.
If this won't work for you, you can specify the connection string in the data context constructor. In your web project have a static class that loads your settings, including your connection strings, and when you create your DAL object (or data context, if creating it directly) just pass it in to the constructor.
public static class GlobalSettings
{
private static string dalConnectionString;
public static string DALConnectionString
{
get
{
if (dalConnectionString == null)
{
dalConnectionString = WebConfigurationManager
.ConnectionStrings["DALConnectionString"]
.ConnectionString;
}
return dalConnectionString;
}
}
}
...
using (var context = new DALDataContext(GlobalSettings.DALConnectionString))
{
...
}
The configuration file for the startup project will define the configuration settings for all included projects. For example if your web project is the startup project, any reference to "appSettings" will look for settings from web.config, this includes any references to "appSettings" from your data access project. So copy any config settings from the Data Access project's app.config to the web project's web.config.
Roll your own ConnectionFactory based on the Registry:
Pro:
Con:
Thanks for the responses.
Those of you who say the app will use the setting in the web.config are correct for instances where I reference it in my own code:
_connectionString = ConfigurationManager.AppSettings["ConnectionString"];
..but there is a different issue with LINQ-SQL datacontexts - I think they include connections strings in the compiled dll for use in the parameterless constructor. As tvanofosson says, I need to create datacontexts by passing in a reference to the connection string in the web.config. Which is where I was getting into a tangle :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With