覆盖machine.config中使用的web.configmachine、config、web

2023-09-06 09:10:34 作者:腥風持枪者

最近我决定把我的连接字符串的machine.config,因为这似乎是迄今为止用于管理多个环境最优雅的方式。不过,我仍想能够在需要的时候在我的本地web.config中覆盖这些设置(或不开明的群众开始抱怨)。

I have recently decided to move my connection strings to machine.config as this seems to be by far the most elegant approach for managing multiple environments. However, I would still like to be able to override these settings in my local web.config if the need arises (or the non-enlightened masses begin to complain).

我怎么能覆盖从machine.config的在我的web.config的设置没有得到一个 ConfigurationErrorsException 因为值已设定?

How can I override settings from machine.config in my web.config without getting a ConfigurationErrorsException because the value has already been set?

推荐答案

如果你想使用本地web.config中,有你在machine.config中已经添加了名称相同添加另一个连接字符串,则首先需要将其删除。元素就像一本字典中的ConnectionStrings,您可以添加替换字符串前添加删除或清除标记。看看这个了解详细信息。

If you're trying to add another connection string using the local web.config that has the same name as one you've added in machine.config, you will need to remove it first. The connectionStrings element works like a dictionary, you can add a remove or clear tag before adding the replacement string. Take a look at this for details.