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!
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:
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):
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="data source=sql.server.address;initial catalog=People;integrated security=False;user id=DbUser;password=DbPassword;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With