代码之家  ›  专栏  ›  技术社区  ›  AGuyCalledGerald Omid.Hanjani

通过web.config覆盖machine.config

  •  17
  • AGuyCalledGerald Omid.Hanjani  · 技术社区  · 15 年前

    我学习使用.NET的内置配置文件提供程序,并具有以下功能 问题:

    我看到machine.config-settings可以被web.config-settings覆盖 一个.NET应用程序。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 回复  |  直到 15 年前
        1
  •  34
  •   Mehrdad Afshari    15 年前

    添加一个 <clear /> 元素作为的第一个子元素 <connectionStrings> . 它将导致配置系统忽略添加到 machine.config 并使用提供的新的。您也可以使用 <remove> 元素来删除单个配置项(如果不想清除全部内容)。

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

    同样的想法也适用于 <providers> 还有部分。