c# - Using Entity Framework app.config how to switch between environments Dev, Stage and Production -
i have windows application accessing dev database using datamodel.edmx , works fine. access stage environment database have added stagedatamodel.edmx. there 2 connection strings in app.config: , how switch between databases in app.config based on environment?
thanks in advance!
normally should other way around - create 1 ef edmx model , 2 (or more) configuration files every environment.
at work, have 3 environments:
- release = production
- stage = before go live (copy of production, final tests)
- debug = new development, dev team tests
for 3 environments, have 3 databases, (almost) similiar each other. create our model dev database. every project communicates database has 3 connection strings different credentials.
in order achieve this, need to:
1) create different build platforms using visual studio configuration manager (in example, there 3 build configurations - dev/stage/release):
2) extract connection string configuration app.settings
file. instead of specifying connection in app.settings
file, use configsource
parameter (the app.config looks this):
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionstrings configsource="app.connectionstrings.config" /> </configuration>
3) create different files every build configuration, named after each build configuration (the wording must exact!) , containing different servers or databases
app.connectionstrings.debug.config
app.connectionstrings.stage.config
app.connectionstrings.release.config
foer example debug can 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) open project settings in visual studio, go build events
, in post-build event command line
tell visual studio take on every build file named after selected build platform , copy specified (in app.config) name output directory:
copy $(projectdir)\app.connectionstrings.$(configurationname).config $(targetdir)app.connectionstrings.config
now when build , start application, configuration depends on build configuration, can debug application connected live environment (when chosen build configuration release
).
more info on how use external configuration files , connection strings, can found in this msdn article.
Comments
Post a Comment