Connection string hell in .NET / LINQ-SQL / ASP.NET

后端 未结 13 1127
渐次进展
渐次进展 2021-01-30 14:29

I have a web application that comprises the following:

  • A web project (with a web.config file containing a connection string - but no data access code in the web pr
13条回答
  •  深忆病人
    2021-01-30 14:43

    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.

    1. Create your own base class, which derives from LinqDataContext

    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
    
    1. Open your DBML file, and in the Properties, add the above class name to the Base Class property.

    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

    1. To ensure the Designer stuff works properly, copy your connection string into the app.config file for the class library. This will not be used apart from in the IDE.

    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

提交回复
热议问题