ConnectionStrings configSource in App.config not working

I am trying to separate the connection string from my App.config , and since you cannot do the conversion, for example, using Web.config , I thought I could use the configSource attribute to point to a different configuration file with the connection string in, but it doesn’t works.

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 it is not, 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 am looking for the simplest solutions.

Any ideas?

+43
c # visual-studio-2012 connection-string
Feb 14 '13 at 10:51
source share
2 answers

If you added the file yourself, the assembly action (in the file properties) may not be set correctly.

The Copy to Output Directory parameter must be Copy if newer or Copy Always to ensure that the .config file is in the bin , otherwise it will not be there, and the attempt to load the configuration will fail.

+112
Feb 14 '13 at 11:32
source share

I had the same problem and the Oded solution works for me. But I’ll just clarify that in order to learn how to change the “Copy to output directory” file so that it is “copied if it’s new or always copy,” you should

  • rigth click on file
  • select properties
  • go to promotion
  • it will see a copy in the output directory and select a copy if it is newer or always copied

It helped me, hope it helps too.

+1
Aug 28 '16 at 20:30
source share



All Articles