Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Entity Framework app.config how to switch between environments Dev, Stage and Production

I have a windows application accessing Dev database using DataModel.edmx and it works fine. To access stage environment database I have added another StageDataModel.edmx. So there are two connection strings in app.config: and How do I switch between databases in app.config based on environment?

Thanks in advance!

like image 923
Poonam E Avatar asked Dec 14 '22 14:12

Poonam E


2 Answers

Normally it should be the other way around - create one EF edmx model and two (or more) configuration files for every environment.

At my work, we have three environments:

  • Release = Production
  • Stage = before GO live (copy of production, final tests)
  • Debug = new development, dev team tests

For the three environments, we have three databases, that are (almost) similiar to each other. We create our model from the DEV database. Every project that communicates with the database has always three connection strings with different credentials.

In order to achieve this, you need to:

1) create different build platforms using the Visual Studio configuration manager (in my example, there are three build configurations - Dev/Stage/Release):

enter image description here

2) extract the connection string configuration from the app.settings file. Instead of specifying the connection in the app.settings file, use the configSource parameter like this (the app.config looks like this):

  <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <connectionStrings configSource="App.ConnectionStrings.Config" />
  </configuration>

3) now create different files for every build configuration, named after each build configuration (the wording must be exact!) and containing different servers or databases

  • App.ConnectionStrings.Debug.config
  • App.ConnectionStrings.Stage.config
  • App.ConnectionStrings.Release.config

Foer example the Debug can look like this:

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
    <add name="Named.ConnectionString" 
            connectionString="metadata=res://*/Abstraction.DbDataContext.csdl|res://*/Abstraction.DbDataContext.ssdl|res://*/Abstraction.DbDataContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sql.server.address;initial catalog=People;integrated security=False;user id=DbUser;password=DbPassword;multipleactiveresultsets=True;App=EntityFramework&quot;"
            providerName="System.Data.EntityClient" />
</connectionStrings>

enter image description here

4) now open your project settings in Visual Studio, go to Build Events and in the Post-Build event command line tell Visual Studio to take on every build the file named after the currently selected build platform and copy it with the specified (in the app.config) name to the output directory:

copy $(ProjectDir)\App.ConnectionStrings.$(ConfigurationName).config $(TargetDir)App.ConnectionStrings.config

enter image description here

Now when you build and start your application, the configuration depends on the build configuration, so you can even debug your application connected to the LIVE environment (when the currently chosen build configuration is Release).

More info on how to use external configuration files and connection strings, can be found in this MSDN article.

A good Entity Framework quick start.

like image 79
keenthinker Avatar answered Feb 23 '23 00:02

keenthinker


I think you are asking how to use different app.config files for debug/release.

Just name them app.Release.config or app.Debug.config and have the debug or release settings in either one.

If its more complicated than that, you can install a tool such as SlowCheetah to modify XML files, you just need to set up different build configurations.

like image 27
Ron Beyer Avatar answered Feb 23 '23 00:02

Ron Beyer