Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connectionStrings configSource in App.config not working

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?

like image 688
Adam K Dean Avatar asked Feb 14 '13 10:02

Adam K Dean


People also ask

How can I get connection string from app config in VB net?

You can simply access it using My. Settings. ConnectionString.

Where are connectionStrings in web config?

Connection strings can be added any where in configuration in such a way it should be a child of configuration. Its recommended that it should be placed after all tags so it remains visible if you need to change it in future.

How do I point to a local config file in configsource?

Solution: In your web.config, use configSource to point to a local config file. Due to .Net restrictions, this must be at or below the level of the root config file. I just point to a file in the app folder itself: In a shared location that is accessible by the application pool user, add the config file containing shared connection strings.

How to retrieve connection strings from application configuration files?

You can use the ConnectionStringSettingsCollection to retrieve connection strings from application configuration files. It contains a collection of ConnectionStringSettings objects, each of which represents a single entry in the connectionStrings section.

How do I store connection strings in an external configuration file?

To store connection strings in an external configuration file, create a separate file that contains only the connectionStrings section. Do not include any additional elements, sections, or attributes. This example shows the syntax for an external configuration file.

Why doesn't the start up project have a link to connectionstring?

The other projects' app.config files also get copied to the output folder. So when you run the start up project, the app.config of the start up project IS in the same folder as connectionString.config. That's why there's no link needed.


2 Answers

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

like image 164
Oded Avatar answered Sep 20 '22 20:09

Oded


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

like image 37
onlyme Avatar answered Sep 16 '22 20:09

onlyme