0

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?

4
  • What's the actual problem? Doesn't ConfigurationSettings.AppSettings("Environment") tell you then environment? It seems you already know how to obtain the right connection string?! Commented May 29, 2016 at 9:34
  • The line of code with ConfigurationSettings.AppSettings("Environment") is from this other project. I guess I want to know how to use it with DbContext file Commented May 29, 2016 at 9:59
  • Can't you just new HOLDbEntities(ConfigurationSettings.AppSettings("Environment") + ...)? No need to modify the context .cs file. Commented May 29, 2016 at 11:59
  • how do i get public TrackerContext() : base("Name=DefaultConnection") {} to have a method to pass in ConfigurationSettings.AppSettings("Environment") ? Commented May 29, 2016 at 22:55

1 Answer 1

1

Try the Configuration Extension perhaps?

Sign up to request clarification or add additional context in comments.

4 Comments

It is a thought I have had, I've used web.config transformations, and then slow cheetah, so I do understand the purpose . I am pretty sure this group that I'm temporarily with is not going to be fond of it as they have their methodologies and with a code review they asked me to use their way of using connection strings ( problem is they are not using dbcontext / ef , and i also do not know how they are managing the proper environment)
Can you just get the connection string however they do it, then just pass it in to your DbContext constructor?
how do i get public TrackerContext() : base("Name=DefaultConnection") {} to have a method to pass in ConfigurationSettings.AppSettings("Environment") ?
Find the code they use to pull the connection string, copy that, then just use it in your constructor for TrackerContext, passing it along to the base, like so: public TrackerContext(connectionString) : base(connectionString) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.