user5299399
user5299399

Reputation:

Single DbContext using multiple connection strings per environment

I know that currently a group of developers that I joined do not use Entity Framework, but they are fine with me using it.

Currently their connection strings are stored in web.config (or 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" />

Now they are telling me to use LH for localhost, then upper environments will they fall in line with DEV and QA.

I'm NOT sure if they are "detecting" the URL for what server as I'm new to the team. I am just trying to get my DbContext in place to consume the proper database connection string.

Currently I just have 1 DbContext with one connection string and thus

public class RPMContext : DbContext

But I see that "they" are using code like this:

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

However, I am wanting to leverage the several connection strings for all the environments with a single DbContext.

I found some code that shows this example web.config

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

then

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

another example I was seeing is

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

Here is a stackoverflow article I had seen, it doesn't really get me to where I want to be though

Entity Framework - using the same DbContext with different connection strings

What do I want? Well I know that I need to have all the environments connection strings in the app.config (not using web.config with this project as it is a console application).

What I do not know is how to know what environment that I am in for the application to be somewhat dummy proof and just "automatically "work"

I'm thinking that reading the URL, but actually it is a console application So I wonder if per server environment if there should be an appsetting changed for which connection string to use so "LH" vs. "DEV" and "QA" and then a few others...

Thoughts?

Upvotes: 0

Views: 1197

Answers (1)

MikeT
MikeT

Reputation: 2663

Try the Configuration Extension perhaps?

Upvotes: 1

Related Questions