62

I'm trying to separate my connection string from my App.config, and as you can't do transformations like with Web.config, I thought may I could use the configSource attribute to point to another config file with the connection string in, but it doesn't seem to be working.

This works, App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=*snip*" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="DefaultConnection"
      providerName="System.Data.SqlClient"
      connectionString="Server=*snip*" />
  </connectionStrings>
</configuration>

But this doesn't, App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=*snip*" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings configSource="connections.config" />      
</configuration>

connections.config:

<connectionStrings>
    <add name="DefaultConnection"
      providerName="System.Data.SqlClient"
      connectionString="*snip*" />
</connectionStrings>

I'm looking for the simplest of solutions.

Any ideas?

5
  • 1
    What is not working? What are the symptoms? What you posted works for me. Are the .config files in the same directory? Commented Feb 14, 2013 at 10:58
  • 1
    The class I am using uses a DbContext which takes a connection string parameter, so I pass to that DefaultConnection. It works when the connectionString is defined in the App.config but not in the separate file. Commented Feb 14, 2013 at 10:59
  • 1
    Make sure both files are saved and in the same directory (or that the relative path is correct in configSource). Commented Feb 14, 2013 at 11:00
  • They're sitting next to each other in the same directory. It's bizarre. Commented Feb 14, 2013 at 11:02
  • 1
    I've put the code into a test console app and I am getting {"The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception."} and {"Unable to open configSource file 'connections.config'. (*project directory*\\bin\\Debug\\TestApp.vshost.exe.config line 17)"} which makes me wonder if it isn't copying the other config file to the build directory... Commented Feb 14, 2013 at 11:06

2 Answers 2

137

If you added the file yourself, the build action (in the file properties) may not have been set correctly.

The Copy to Output Directory option needs to be Copy if newer or Copy Always to ensure that the .config file ends up in the bin directory, otherwise it will not be there and trying to load the configuration will fail.

Right Click on file and then Click properties

enter image description here

Change to "Copy always" or "Copy if newer"

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Note: Thus, in some web apps, you will have too add reference to: configSource="bin\connections.config" instead of configSource="connections.config"
I was facing the same issue and before finding this post, I suspected this to be an output copying problem. So I went ahead and first checked out app.config properties, which apparently are set to None and Do not copy. Why does this setting not work for ConnectionStrings.config?
@dotNET App.config has an special treatment during build event. It's transformed (if any transform is defined), renamed to match exe name's and copied to output folder as part of msbuild task
I also had to set my connections.config "Build Action" to Content... then it actually copied it to the root (where I was expecting it).
I had same issue, I was using custom configuration section. Problem was path which I specified was relative to app.config file, but in order for it to work path should be relative to <dllname>.dll.config location. It fixed the issue. It was unable to find that file in previous location.
3

I had the same issue and Oded solution works for me. But I will just precise that to find out how to change the file "Copy to Output Directory option" to be "copy if newer or copy always", you have to

  • rigth click on the file
  • select properties
  • go to advance
  • then will see copy to output directory and choise copy if newer or copy always

This helped me, I hope it will help you too

1 Comment

Go to advance, if you are using Categorized Properties Panel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.