1

Normally, in a .NET application, you store your database connection strings in the app.config.

For a single .exe, there's a single .exe.config. And we're using Entity Framework, which defaults to using a .config setting that matches the DbContext name. Which all works fine, so long as you follow the common pattern of having a single instance of an application talking to a single database.

But our problem requires we have multiple instances of our application, each with its own distinct database connection.

Possible solutions:

  1. Create multiple copies of the .exe, each with its own copy of the .config, and put a different connection string in each. Advantage? No coding changes. Disadvantage? Maintaining all the different copies, making sure they all get updated, etc.
  2. Keep one copy of the .exe, with one copy of the .config, and put multiple connection strings in the .config. Then pass a command-line argument to the program that selects which connection string to use. Advantage? Only one .exe to manage. Disadvantage? Coding change, likelihood that someone's going to mess up the command-line arguments.
  3. Some other brilliant idea, that someone here on Stack Overflow is going to point out to me.

Any ideas?

I'm sure people have dealt with this issue, before. Is there a common way of handling the issue?

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
  • How often will they be re-run? If not often then definitely better option. Also, how much effort would a coding change be? – nicV Jul 28 '15 at 14:04
  • These are background services, either running in a command window or as windows services, as the customer chooses. Either way, they need to be running around the clock. – Jeff Dege Jul 28 '15 at 14:56
  • Well in that case they won't be restarted that often, option 2 is best. Add some validation that the option entered worked and let them be! In the long run the code change will be worth the only one config to manage! – nicV Jul 29 '15 at 07:35

2 Answers2

2

You can also think of creating copies of config file and load the appropriate one based on command line parameter using following code:

AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file")

See this for detail: Relocating app.config file to a custom path

Community
  • 1
  • 1
Deepak Bhatia
  • 1,090
  • 1
  • 8
  • 13
  • I'm not sure that would be easier to manage than having one app.config with multiple connection strings (my choice #2), but it is an actual alternative I'd not considered, which is what I was asking for, so it's worth an upvote. – Jeff Dege Jul 29 '15 at 15:36
0

You could try something like this within some C# applications to change the connection string per application.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringSection.ConnectionStrings["database"].ConnectionString = string.Format("Data Source={0}", userCon);
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
Kevin Mee
  • 539
  • 3
  • 14
  • At first glance, that looks like it's changing the connection string in the app.config. And that doesn't fix the problem. – Jeff Dege Jul 28 '15 at 21:39