Can I override the connection string in web.config for local development?

I have a WebForms project that has a connection string hardcoded in web.config (for Debug - web.Debug.config). This connection string points to the database server for development.

I would like to run a local copy of this database so that my changes do not immediately affect others.

What I did is go into web.config and update the connection string to point to my local DB. This works, but it is somewhat tedious, since I have to remember to exclude web.config and must re-update it if I cancel all the changes.

Since, as I said, others use this solution, I would like not to check anything or change the web configuration.

Is there a way to override the connection string web.config to force the site to point to my local DB without checking anything in the original control?

+6
source share
3 answers

Create SQL ALIAS on the local computer.

Click "Start" and type "cliconfg.exe" on the local computer. This will help you create an SQL alias on your local computer. On web.config, connect to the live database, but on your computer, create an SQL alias that redirects your local database. When this is done, when you publish the program, it will automatically connect to the live database, but when launched from your local computer, it will connect to the local database without changing the code.

+3
source

One solution would be to use a configuration conversion function.

VS 2012:

  • Go to Build-> Configuration Manager
  • Click on the Active Solution Configuration List field and select Create.
  • Enter the local name for the new configuration and click "Save."
  • Make sure you change the values ​​in the "Configuration" column to "Debug" or have ever been previously
  • Right-click the Web.config file and select Add Config Transform, which will add the newly created web.local.config
  • Inside web.local.config, add a local connection string conversion, something like

    <connectionStrings> <add name="DbConnection" connectionString="...Add Local Connection String..." xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> 

When you start or debug a project, make sure you select a local configuration from the list next to the green arrow. You just do not check the web.local.config file

+9
source

One thing you can possibly do is to have a separate file for your connection strings - you would not check this at all for the source code.

Depending on how your materials are organized, you may need everyone doing development to do the same, and depending on how you publish / deploy, this may not work for you.

For instance:

Web.config

 <configuration> <connectionStrings configSource="connectionstrings.config"> </connectionStrings> </configuration> 

connectionstrings.config (not in the control source)

 <connectionStrings> <add name="cs" connectionString="server=.;database=whatever;"/> </connectionStrings> 

Each developer can choose which database their local machine belongs to, and as long as the connectionstrings.config file is not in the original control (add it to the ignore list), no one will step on each other.

+6
source

Source: https://habr.com/ru/post/952156/


All Articles