1

I am setting up a dev, qa, staging, production system of deployment. I would like to be able to promote a release from one environment to the next without having to re-publish from VS, and without manually touching any files.

I need separate databases for DEV, QA and STG/PRO , so this would mean connection strings need to be switched dynamically according to the environment.

I could do this in a data layer -- perhaps something similar to this: Managing ASP.NET Development, Staging and Production Connection Strings (without pulling your hair out) -- but my data layer is built upon Entity Framework.

QUESTION: Is there a way to achieve dynamic switching of connection strings while using Entity Framework?

1

2 Answers 2

1

I am setting up a dev, qa, staging, production system of deployment. I would like to be able to promote a release from one environment to the next without having to re-publish from VS, and without manually touching any files.

This is really strange and bad requirement. It is absolutely common to reconfigure application during deployment to different environment. Instead of hardcoding this in your application you should have different set of installation / deployment scripts which would also change your configuration file when moving from one environment to another.

Holding configuration for all environments in the configuration is IMHO very bad practice.

Even with hardcoded solution you still need to change some "configuration" to tell application which environment it currently runs on. Hardcoded solution will use information about environment to select correct connections string from configuration file and pass it to context constructor.

As example of the mentioned approach you can try this. It will still require you to change environment variable each time you redeploy application - complexity of such modification in custom deployment script is exactly the same as replacing connection string:

Configuration file:

<appSettings>
  <add key="environment" value="Dev"/>
</appSettings>
<connectionStrings>
  <add name="Dev" connectionString="..."/>
</connectionStrings>

Code for context factory method:

public static YourContext ContextFactory()
{
    string environment = WebConfigurationManager.AppSettings["environment"].Value;
    // This should be correctly recognized as a name of connection string.  
    return new YourContext(environment);  
}

Context:

public class YourContext : DbContext
{
    public YourContext(string connectionStringName) : base(connectionStringName)
    { }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This may be a valid comment, but it is not an answer to my question. Please move to a comment. "Answering" with non-answers discourages others from answering.
The last paragraph is answer to your question. The rest is simply explanation why you are doing it wrong. That is the reason why you asked the question in the first place, didn't you? To learn how to do it correctly ...
Also what if "The hosting provided requires that an identical code base be used on staging and production, including connection string" (as in this post: stackoverflow.com/questions/7434753/…)
I know I "need to change some 'configuration' to tell application which environment it currently runs on." My question regards how you do this (pass it into) Entity Framework.
Ok. I added some example which I guessed should be obvious from the description.
|
0

Assuming that you are using a unit of work pattern; it means that your object context is recreated after every unit of work. You likely have a class, that inherits from some sort of object context so in the constructor you use to create that context you can reference the base constructor that allows you to pass in a connection string. From there, you can call a static method or new up an object to handle the creation of a connection string or pass in an entity connection.

If you are using a DbContext, it is the same, only with DbConnection instead of EntityConnection.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.