每个环境使用多个连接字符串的单个 DbContext

Single DbContext using multiple connection strings per environment

我知道目前我加入的一组开发人员不使用 Entity Framework,但他们对我使用它没有意见。

目前他们的连接字符串存储在web.config(或app.config

<add name="LH_RPMDB" 
     connectionString="data source=localhostdb;Database=dbsim;user id=sim;password=sim;Application Name=SIM-LH;" 
     providerName="System.Data.SqlClient" />
<add name="DEV_RPMDB" 
     connectionString="data source=devdb;Database=dbsim;user id=sim;password=sim;Application Name=SIM-dev;" 
     providerName="System.Data.SqlClient" />
<add name="QA_RPMDB" 
     connectionString="data source=qadb;Database=dbsim;user id=sim;password=sim;Application Name=SIM-qa;" 
     providerName="System.Data.SqlClient" />

现在他们告诉我本地主机使用LH,那么上层环境是否符合DEVQA

我不确定他们是 "detecting" 还是 URL 用于哪个服务器,因为我是团队的新手。我只是想让我的 DbContext 到位以使用正确的数据库连接字符串。

目前我只有 1 个 DbContext 和一个连接字符串,因此

public class RPMContext : DbContext

但我看到 "they" 正在使用这样的代码:

ws.Url = ConfigurationSettings.AppSettings(ConfigurationSettings.AppSettings("Environment") + "_SNTRAX")

但是,我想利用单个 DbContext 为所有环境使用多个连接字符串。

我找到了一些显示此示例的代码 web.config

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString ...

然后

public TrackerContext() : base("Name=DefaultConnection") {}

我看到的另一个例子是

public partial class HOLDbEntities
{
    public HOLDbEntities(string connectionString)
        : base(connectionString) 

这是我看过的一篇 Whosebug 文章,但它并没有真正让我到达我想去的地方

Entity Framework - using the same DbContext with different connection strings

我想要什么?好吧,我知道我需要在 app.config 中包含所有环境连接字符串(不对这个项目使用 web.config,因为它是一个控制台应用程序)。

我不知道的是如何知道我所处的应用程序在什么环境下是某种虚拟证明并且只是 "automatically " 工作"

我正在考虑阅读 URL,但实际上它是一个控制台应用程序所以我想知道每个服务器环境是否应该更改应用程序设置以使用哪个连接字符串 "LH" 与 "DEV" 和 "QA" 以及其他几个...

想法?

也许试试 Configuration Extension