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