通过web.config覆盖machine.config

17

我正在学习如何使用.Net的内置配置文件提供程序,并遇到了以下问题:

我了解到,.Net应用程序的web.config设置可以覆盖machine.config设置。对我来说,machine.config文件中的以下设置是相关的:

<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>

这些设置用于设置本地配置文件。但是,当我将这些设置复制到我的应用程序的web.config中并更改machine.config中的设置时,它们就不能正常工作了,我得到了一个配置错误。例如,我将machine.config中提供程序的名称更改为“Local”。这应该不是问题,因为这些设置会被覆盖。然而,在运行应用程序时,我遇到了以下错误:

"条目“AspNetSQLProvider已经添加”(我的翻译)

1个回答

35

<connectionStrings>的第一个子元素添加一个<clear />元素。这将导致配置系统忽略在machine.config中添加的所有连接字符串并使用提供的新连接字符串。如果您不想清除整个内容,还可以使用<remove>元素来删除单个配置项。

<connectionStrings>
   <clear />
   <add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/> 
</connectionStrings>
同样的想法也适用于<providers>部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接