基于上下文的实体框架的不同连接字符串 [英] Different Connection Strings with Entity Framework based on Context

查看:162
本文介绍了基于上下文的实体框架的不同连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用实体框架的Web表单应用程序,该应用程序部署在开发框,本地计算机和生产箱上。它们中的每一个具有不同的连接字符串。
最好的处理方式是什么。

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.

我使用TFS Build Server来部署到开发中,并将该压缩文件的结果复制到

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

我也使用Web部署项目,如果这有助于

I also use Web Deployment Projects if that helps

我以前做的是当ORM启动后,将根据根文件夹的名称选择一个连接字符串。使用实体框架我不知道如何做到这一点,而不必在每个页面上设置。

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.

推荐答案

类似的,我创建了一个类来包装EntityContext对象,它根据你如何设置你的连接字符串来适当地设置连接字符串 - 你需要类似的东西:

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

这篇关于基于上下文的实体框架的不同连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆