Different Connection Strings with Entity Framework based on Context

和自甴很熟 提交于 2019-11-30 14:25:16

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/1453701/different-connection-strings-with-entity-framework-based-on-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!