Reputation: 15404
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?
Upvotes: 1
Views: 2645
Reputation: 364279
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)
{ }
}
Upvotes: 1
Reputation: 1317
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
.
Upvotes: 0