Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

asp.net - Override machine.config by web.config

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...