Tony Barsotti
Tony Barsotti

Reputation: 1691

Two different connection strings for web.config in Git

I'm fairly new to Git and I'm currently migrating a project that was in SVN to Git. The way that it currently works is that there is a dev directory on the server and a production directory on the server. Dev and production each have a separate database and thus have a different connection string in their respective web.config's. I'm going to be using the gitflow workflow so I'll be merging dev into master when changes are ready for production so I'll somehow need to have the web.config on the dev branch have one connection string and have a separate connection string on the production branch. Is there any logical way of doing this?

Upvotes: 6

Views: 3232

Answers (3)

Dara Oladapo
Dara Oladapo

Reputation: 596

Here is an instance of how I would do it...

Editing my Web.Config file
Note: the names: DefaultConnection, LocalConnection, livedbsource, localdbsource, localMachineName

 <connectionStrings>
 <!--Live-->
 <add name="DefaultConnection" connectionString="Data Source=livedbsource;Initial Catalog=livedbname;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
<!--Dev-->
<add name="LocalConnection" connectionString="Data Source=localdbsource;Initial Catalog=localdbname;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>

And for the ApplicationDbContext

static string ServerName = System.Net.Dns.GetHostName();
//setting what connection string to use - tenary operator
static string ConnectionStringID = 
        (ServerName == "localMachineName") ? "LocalConnection" : "DefaultConnection";
public ApplicationDbContext(): base(ConnectionStringID, throwIfV1Schema: false)
{
}

On Windows 7 and higher, You can get the localMachineName pressing the Windows Logo and searching for System Information, then look for 'System Name'

Here is the YouTube video I did it in Visual Studio 2017 on a live project and it worked.

https://youtu.be/oKg6ewKhkYs

Upvotes: 3

Steven V
Steven V

Reputation: 16595

In asp.net you may want to look into web.config transformation. There's probably already a web.release.config and a web.debug.config in you project. They can be used to switch the connection string depending on which build configuration (build, debug, etc) you use to build your application.

Another idea which I've run into is including your connection strings in a separate file like:

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

which would cause .NET to load the connection strings from the connectionStrings.config file. Then that file is ignored in version control, and the dev version is distributed in the local development environments, the staging version is placed on the staging server and then the production one stays in production.

There are definitely more solutions than just these out there. But I've had decent luck with both of these options in a smaller gitflow type environment.

Upvotes: 9

Gaui
Gaui

Reputation: 8959

You should use Web.config Transformations for deployment based configs. It's okay for them to travel between branches, because they should all be the same on all branches.

However for local development, the Web.config, Git Attributes is what you should use.

Just create a .gitattributes file in the root of your project (where the .git/ folder is) and put the following line in the file:

Web.config merge=ours

Each time you merge a branch with a updated Web.config file, it will keep your current changes.

Upvotes: 4

Related Questions