17

I learn to work with the built-in profile provider of .Net, and have the following problem:

I read that the machine.config-settings can be overridden by the web.config-settings of a .Net-Application. The following settings in the machine.config-file are relevant for me:

<connectionStrings>
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;
Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

<profile><providers><add name="AspNetSqlProfileProvider"connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/></providers></profile>

These settings work to set up local profiles. However, when I copy the settings into the web.config of my application and change the machine.config settings, so that they don´t work any more, I get a configuration error. For example, I change the name of the provider in the machine.config to "Local". This should be no problem, because the settings are overridden. However, when running the application I get the error:

"The entry "AspNetSQLProvider has already been added" (my translation)

Çağdaş Tekin
  • 16,592
  • 4
  • 49
  • 58
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120

1 Answers1

35

Add a <clear /> element as the first child of <connectionStrings>. It'll cause the configuration system to ignore all connection strings added in machine.config and use the new ones provided. You can also use <remove> element to remove a single configuration item if you don't want to clear out the whole thing.

<connectionStrings>
   <clear />
   <add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/> 
</connectionStrings>

The same idea applies to <providers> sections as well.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789