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!
configwith the connection to the file you want. I'm pretty sure that doesn't override your default config file, though.