1

I have this app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Me.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <connectionStrings>
    <add name="MyConnectionString01" connectionString="...." />
    <add name="MyConnectionString02" connectionString="...." />
  </connectionStrings>  

  <applicationSettings>
    <Me.Properties.Settings>
      <setting name="BaseDatosMedioAcceso" serializeAs="String">
        <value>SQLServerEF6</value>
      </setting>
    </Me.Properties.Settings>
  </applicationSettings>


  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup>
</configuration>

But I would like to add this kind of settings:

<DataBases>
   <add Description="Local" ConnectionString="MyConnectionString01"/>
   <add Description="Local (Test)" ConnectionString="MyConnectionString02"/>
</DataBases>

But I don't know where tu put this configuration. I have tried to create a config section, but It does not work.

The idea is to have a description that is easy to read for the user and relate this description with the connection string to use with entity framework.

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

2

To add generic custom key's you can use the following:

<configuration>
  <appSettings>
    <add key="Local" value="MyConnectionString01" />
    <add key="LocalTest" value="MyConnectionString02" />
  </appSettings>
</configuration>

To specifically add database connection strings use the following:

<configuration>
  <connectionStrings>
     <add name="Local" connectionString="MyConnectionString01"/>
     <add name="LocalTest" connectionString="MyConnectionString02"/>
  </connectionStrings>
</configuration>

Offhand, I'm not sure if the spaces/special characters (i.e. "Local (Test)") would be allowed in the key or name parameter. I've never seen anyone use any there. I'd use LocalTest instead. http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx

  • Why if I use the label "appSettigns" it works and when I use the label "DataBases" it does not work? – Álvaro García Nov 08 '14 at 15:23
  • 1
    The Web.config file must contain only entries for configuration items that override the settings in the Machine.config file. Some additional reading that might help: http://support.microsoft.com/kb/815179#3 | http://msdn.microsoft.com/en-us/library/ms960520(v=cs.70).aspx | http://msdn.microsoft.com/en-us/library/ms178685(v=vs.100).aspx – Matt Lengenfelder Nov 08 '14 at 15:53