I have a web application that comprises the following:
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))
{
...
}