Setup connection strings in a personal.config file

How to setup your personal.config file with your connection string.
The purpose of this is that every developer can have their own connection strings saved in a personal.config file that does NOT get checked into source control. This way your passwords
are not in source control and every developer can setup the databases as they want on their own machine.
1) First of all you need to change the web.config to have the connection strings section look like this:
  <connectionStrings configSource=personal.config >
2) Now you need to add a personal.config file somewhere in your project (I didn’t have any success putting it in the bin folder so I just put it in my website’s root. The contents of the file will be this:
       < add name =MyDbContext providerName=System.Data.SqlClient connectionString=Server=localhost;Database=MyDatabase; Integrated Security=false;User=sa;Password=mypassword />
3) Right click the file in Solution Explorer and set
     Copy to Output Directory = “Copy always”  (so it will copy the config file to ‘obj/default’ folder)
4) Now you need to right click the personal.config file and select “Exclude From Project” (so it won’t be saved in source control).
5) Finally let’s make sure that GIT is ignoring this file by opening ‘Team Explorer’.
     Select ‘Changes’.
     If the file is showing in the list of changes, here, then right click ‘personal.config’ and select ‘Ignore this local item’. (the file will disappear and a file called ‘.gitignore’ will appear in change list.
6) Verify your code runs and commit your changes.
NOTE: Even though I have set Copy to Output Directory = “Copy always”, I don’t see the file appear in the obj/debug folder, but this does make it work properly on my machine.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s