如何使这两个web.config转换正常工作?

时间:2019-02-01 15:17:04

标签: c# .net web-config web.config-transform

我创建了一个名为 Local 的配置,并希望对我的web.local.config进行以下转换:

1。连接字符串

我的web.config具有以下连接字符串:

<connectionStrings>
    <add name="Entities" 
         connectionString = "dummy"
         providerName="System.Data.EntityClient" />
</connectionStrings>

不过,我希望我的web.local.config具备以下条件:

<connectionStrings>
  <add name="Entities"
     connectionString="What a nice connection string!"
     providerName="System.Data.EntityClient" />
</connectionStrings>

我目前已这样设置转换:

<connectionStrings>
<add name="Entities"
     connectionString="What a nice connection string!"
     xdt:Transform="SetAttributes"
     xdt:Locator="Match(name)"/>
</connectionStrings>

我还尝试了如下的Replace转换,但这也不起作用:

<connectionStrings>
  <add name="Entities"
    connectionString="What a nice connection string!"
         xdt:Transform="Replace"
         xdt:Locator="Match(name)"/>
</connectionStrings>

但是当我的DbContext对象尝试使用我的连接字符串初始化自身时,仍然在Entity Framework中出现格式异常。

2。应用设置

我的web.config具有以下应用设置:

<appSettings>
    <add key="Nice" value="true" />
    <add key="NotNice" value="true" />

    <!-- I want only this one's value to change in my web.local.config -->
    <add key="foo" value ="I am a foo." />
</appSettings>

我想改变该应用设置,其关键是只值foo,使我的appSettings在我的web.local.config如下所示:

<appSettings>
    <add key="Nice" value="true" />
    <add key="NotNice" value="true" />

    <!-- I want only this one's value to change in my web.local.config -->
    <add key="foo" value ="Are you also a foo?" />
</appSettings>

我当前应用的转换看起来像这样:

<appSettings>
    <add key="foo" value = "Are you also a foo?"
     xdt:Transform="SetAttributes(value)"
     xdt:Locator="Match(key)"/>
</appSettings>

我还尝试了如下的Replace转换,但这也不起作用:

<appSettings>
  <add key="foo" value = "Are you also a foo?"
  xdt:Transform="Replace"
  xdt:Locator="Match(key)"/>
</appSettings>

但是,当我在调试模式下运行 Local 配置时,从foo中读取密钥appSettings时,代码仍会读取旧值"I am a foo."

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用“预览转换”功能? enter image description here

此外,检查您的项目文件以查看是否存在与以下内容类似的行:

enter image description here

请参阅SlowCheta Tool页以获取更多信息。

相关问题