Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a WebForms project that has a connection string hard coded into the web.config (for Debug - web.Debug.config). This connection string points to a DB server for development.

I'd like to run a local copy of that database so that my changes don't immediately affect others.

What I've been doing is going into the web.config and updating the connection string to point to my local DB. This works, but is somewhat tedious as I have to remember to exclude the web.config and have to re-update it if I undo all changes.

Since, like I said, others are using this solution, I'd like to avoid checking anything in or modifying the web config.

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

like image 892
Mike Davis Avatar asked Aug 20 '13 20:08

Mike Davis


Video Answer


2 Answers

One thing you might be able to do is have a separate file for your connection strings - you wouldn't check this into source control at all.

Depending on how your stuff is organized, you might need everyone who's doing development to do the same thing, and depending on how you do your publish/deploy, this may not work for you.

For example:

Web.config

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

connectionstrings.config (not in source control)

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

Each developer can choose which database their local machine points to, and as long as the connectionstrings.config file is not in source control (add it to the ignore list), nobody will step on each other's feet.

like image 83
Joe Enos Avatar answered Nov 08 '22 08:11

Joe Enos


Create a SQL ALIAS on your local machine.

Click start run and type "cliconfg.exe" on local machine. This will help you to create a SQL Alias on your local machine. On the web.config connect to the live database but on your machine create a SQL Alias that will redirect to your local database. When this is done, when you publish the program it will automactically connect to the live database but when running from your local machine it will connect to local database without any code change.

like image 30
Cherian M Paul Avatar answered Nov 08 '22 08:11

Cherian M Paul