Different Connection Strings with Entity Framework

2019-02-07 18:34发布

I have a web forms application that uses entity framework, the application is deployed on a development box, my local machine and a production box. Each of these have different connection strings. What is the best way of handling this.

I use TFS Build Server to deploy to development and take the result of that build zip it and copy it to production manually.

I also use Web Deployment Projects if that helps

What I was doing before was when the ORM started it would choose a connection string based on the name of the root folder. With Entity Framework I don't know how to do this without having to set it on every page.

2条回答
The star\"
2楼-- · 2019-02-07 18:57

FYI You can use config transformations now in VS 2010: http://msdn.microsoft.com/en-us/vstudio/Video/ff801895

查看更多
放荡不羁爱自由
3楼-- · 2019-02-07 19:06

We have something vaguely similar, I created a class to wrap the EntityContext object, which sets the connection string appropriately - you'd need something similar, based on how you set your connection string:

Public Class MyEntityModel

    Private _dataContext As Entities

    Public Sub New()

        Dim entityBuilder As New EntityConnectionStringBuilder()

        entityBuilder.ProviderConnectionString = MyApplicationConnectionString

        entityBuilder.Metadata = "res://*/"

        entityBuilder.Provider = "System.Data.SqlClient"

        _dataContext = New Entities(entityBuilder.ConnectionString)

    End Sub

    Public Function DataContext() As Entities
        Return _dataContext
    End Function

End Class
查看更多
登录 后发表回答