3

I have a web application that has a Web.config file, but I need it to load several sections from another config file that's located in a defined location (not my design!). One of these sections is connectionStrings. I've tried the following, but I get an error when my web service loads saying System.InvalidOperationException: No connection string named 'MyDataModel' could be found in the application config file.

ConfigurationFileMap map = new ConfigurationFileMap(@"C:\conf1.conf");
config = ConfigurationManager.OpenMappedMachineConfiguration(map);

ConfigurationManager.RefreshSection("connectionStrings");

Here is the relevant part of my config file:

    <configSections>
    <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyDataModel" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;
      ..."/>
  </connectionStrings>

I'm aware that I can "include" a config file snippet in another config file, but that won't work here because the file that would be included contains several other sections. Is there a way that I can get the runtime to properly load my external config file? Thanks!

SoxFan44
  • 147
  • 1
  • 1
  • 7
  • I don't see any portion of your code that's trying to read MyDataModel, can you include that? – cost Jul 14 '14 at 17:23
  • It's automatically loaded by entity framework, there is no explicit call to read it. – SoxFan44 Jul 14 '14 at 17:26
  • 1
    Oh, that's an important thing you should have added. I honestly don't know if you can use multiple config files in that way, it depends how EF is getting the connection string under the hood. Perhaps read it in manually and pass the connection string to your EF constructor? – cost Jul 14 '14 at 17:28
  • Even taking EF completely out of the picture, after I call ConfigurationManager.OpenMappedMachineConfiguration(map); I don't see the ConnectionStrings property populated. I would expect (perhaps incorrectly) that this would be populated automatically. – SoxFan44 Jul 14 '14 at 17:46
  • 1
    Right now you just have a file `config` with the connection to the file you want. I'm pretty sure that doesn't override your default config file, though. – cost Jul 14 '14 at 17:53
  • Another way you could handle this is to override the DBContext's constructor so that it gets the appropriate connection string from the external config and passes it into the base constructor – cost Jul 14 '14 at 20:33

1 Answers1

1

You are on the right track with using the ConfigurationManager to read a mapped config. The next bit is that you have to manually grab the keys when you want them. Here is a very similar question where the answer addresses the manual reading of the stuff you want:

Equivalent to 'app.config' for a library (DLL)

Copied from the above link:

string GetAppSetting(Configuration config, string key)
{
    KeyValueConfigurationElement element = config.AppSettings.Settings[key];
    if (element != null)
    {
        string value = element.Value;
        if (!string.IsNullOrEmpty(value))
            return value;
    }
    return string.Empty;
}

EDIT:

Since EF only looks at your local config (AFAIK), what about generating your web.config using a T4 template? You can then pipe in the necessary data from the remote config file. I found a good article on how to do this:

http://www.geoffhudik.com/tech/2010/10/19/webconfig-automation-with-t4-and-a-macro.html

EDIT2:

Is using metadata in your EF connection string an option? If so, you can point it at that other assembly without having to mess with ConfigurationManager:

http://msdn.microsoft.com/en-us/library/vstudio/cc716756(v=vs.100).aspx

Community
  • 1
  • 1
Bill Sambrone
  • 4,334
  • 4
  • 48
  • 70
  • The problem is that I'm using entity framework, which loads this under the hood somehow. I have existing code that grabs specific keys and that works just fine, but it seems like EF only wants to use connection strings in the app.config file. – SoxFan44 Jul 14 '14 at 17:44
  • Ah, that is a tough one. You could use a transform to modify your web.config for your release build, but then you have the problem of the connection string being defined in 2 places. I'm looking to see if web.config transforms can work by pulling data in from a separate config file. – Bill Sambrone Jul 14 '14 at 18:35
  • Turns out you can pass in a connection string to the DbContext constructor. Since your answer led me down that path I'll mark yours as correct. Thanks! – SoxFan44 Jul 15 '14 at 15:44