I have a web application that comprises the following:
I know this is old but here's how I do it (I quite like @Seba's way but I haven't tried that)
This assumes your DBML file resides in its own class library, which I've found it most convenient when sharing entities and data access across multiple websites, and other class libraries. It also assumes you've named your connection string the same in each project. I use NAnt to set this when I deploy to different environments.
I based this on the top answer above from @tvanfosson - kudos to that guy.
Here's the VB code:
Imports System.Configuration
Public Class CustomDataContextBase
Inherits System.Data.Linq.DataContext
Implements IDisposable
Private Shared overrideConnectionString As String
Public Shared ReadOnly Property CustomConnectionString As String
Get
If String.IsNullOrEmpty(overrideConnectionString) Then
overrideConnectionString = ConfigurationManager.ConnectionStrings("MyAppConnectionString").ConnectionString
End If
Return overrideConnectionString
End Get
End Property
Public Sub New()
MyBase.New(CustomConnectionString)
End Sub
Public Sub New(ByVal connectionString As String)
MyBase.New(CustomConnectionString)
End Sub
Public Sub New(ByVal connectionString As String, ByVal mappingSource As System.Data.Linq.Mapping.MappingSource)
MyBase.New(CustomConnectionString, mappingSource)
End Sub
Public Sub New(ByVal connection As IDbConnection, ByVal mappingSource As System.Data.Linq.Mapping.MappingSource)
MyBase.New(CustomConnectionString, mappingSource)
End Sub
End Class
Note, if you placed the custom data context class in the same assembly, simply include the class name, e.g. CustomDataContext.
If they are in different assemblies, use the fully qualified name, e.g. MyCo.MyApp.Data.CustomDataContext
That's it.
You'll need to name your connection string the same
What you are essentially doing is forcing the data context to ignore the connection info set in the DBML file. Using the ConfigurationManager methods will mean that it will pick up the connection string from the calling assembly.
HTH
I had a bit of a struggle with this issue too. I found a solution by using c# partial class definition and extending the datacontext created by dbml designer. This solution quite is similar to tvanfosson's answer. What you have to do is to create partial datacontext class with default constructor getting ConnectionString from settings and in dbml designer DC properties set connection to None. That way connection string will not be the compiled into dll. Datacontext will automatically get connection string from web.config connectionstring settings. I have not tested if this works with app.config also, but I think it should work fine.
Here is sample of partial DC class:
namespace MyApplication {
/// <summary>
/// Summary description for MyDataContext
/// </summary>
///
public partial class MyDataContext
{
public MyDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
}
}
You could also have the web application provide the connection string when it needs to use the Data Access project. You could make it part of the constructor.
Also, you could could just write your own logic to load a connection string from an external file when the data access project makes it's calls.
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))
{
...
}
Your application will only use the config entries in the web.config file. You can put dll config setting in the web.config file as long as they are structure properly. My example is VB specific using the My Namespace, but it gives you the general idea.
In the configSections paret of the config file you will need an entry:
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="YourAssembly.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup></configSections>
Then in the applicationSettings part of the config file you put the entries for each dll:
<applicationSettings>
<YourAssembly.My.MySettings>
<setting name="DebugMode" serializeAs="String">
<value>False</value>
</setting>
</YourAssembly.My.MySettings>
</applicationSettings>
Roll your own ConnectionFactory based on .config files:
Pro:
Con: