【问题标题】:asp.net web.config transform not workingasp.net web.config 转换不起作用
【发布时间】:2014-10-06 14:04:56
【问题描述】:

我在一个项目中针对不同的环境做了几个配置,一个是debug,另一个是release

Web.config:

<configuration>
  <connectionStrings>
    <add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.7.131;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=debugMe;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="FilePath" value="D:\temp"/>
    <add key="debugMode" value="Y"/>
  </appSettings>
</configuration>

Web.debug.config:

   <?xml version="1.0" encoding="utf-8"?>

    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <!--
        In the example below, the "SetAttributes" transform will change the value of 
        "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
        finds an attribute "name" that has a value of "MyDB".

        <connectionStrings>
          <add name="MyDB" 
            connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
            xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
        </connectionStrings>
      -->
      <system.web>
        <!--
          In the example below, the "Replace" transform will replace the entire 
          <customErrors> section of your web.config file.
          Note that because there is only one customErrors section under the 
          <system.web> node, there is no need to use the "xdt:Locator" attribute.

          <customErrors defaultRedirect="GenericError.htm"
            mode="RemoteOnly" xdt:Transform="Replace">
            <error statusCode="500" redirect="InternalError.htm"/>
          </customErrors>
        -->
      </system.web>
  <connectionStrings xdt:Transform="Replace">
        <add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.7.131;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=debugMe;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    </configuration>

Web.live.config:

   <?xml version="1.0" encoding="utf-8"?>

    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <!--
        In the example below, the "SetAttributes" transform will change the value of 
        "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
        finds an attribute "name" that has a value of "MyDB".

        <connectionStrings>
          <add name="MyDB" 
            connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
            xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
        </connectionStrings>
      -->
      <system.web>
        <!--
          In the example below, the "Replace" transform will replace the entire 
          <customErrors> section of your web.config file.
          Note that because there is only one customErrors section under the 
          <system.web> node, there is no need to use the "xdt:Locator" attribute.

          <customErrors defaultRedirect="GenericError.htm"
            mode="RemoteOnly" xdt:Transform="Replace">
            <error statusCode="500" redirect="InternalError.htm"/>
          </customErrors>
        -->
      </system.web>
  <connectionStrings xdt:Transform="Replace">
        <add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    </configuration>

问题来了:

每当我使用调试或发布配置时,连接字符串仍然是 web.config 中的连接字符串,而不是 web.release.config 中的连接字符串

是不是我做错了什么?

【问题讨论】:

    标签: asp.net database-connection app-config


    【解决方案1】:

    有时使用逐项转换会更好。而不是部分替换使用

    <add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
    

    当您在节点中有很多属性并且只需要更改一个时,使用这种方法非常有用,例如您只需使用 SetAttributes 而不是 Replace。

    【讨论】:

    • 非常感谢您的建议..但问题是......配置转换前后似乎没有区别......连接仍然是旧的......跨度>
    • 您可以打开 Visual Studio 中的选项 - 项目和解决方案 - 构建和运行 MSBuild 项目构建输出详细程度,并在那里找到您的配置转换。它会告诉你什么是错的。通常它看起来像“因为...而跳过转换”
    • 工具 - 选项 - 项目和解决方案 - 构建和运行,VS2013中有MSBuild项目构建输出详细度组合框。 blogs.msdn.com/b/saraford/archive/2008/10/07/…
    【解决方案2】:

    您的发布构建设置是否使用 Web.live.config 文件?默认情况下,Release 需要一个名为 Web.Release.Config 的文件,我只是提到这一点,因为该文件被列为 Web.live.config,但您称它为“Release”。如果这只是一个拼写错误并且名称一致,或者如果您的发布构建配置设置为“实时”,那么请忽略它。

    【讨论】:

      【解决方案3】:

      对我来说,转到转换文件的属性并更改属性; Copy to Output DirectoryCopy Always 成功了。得到了Matt Kogaj's answer的提示

      【讨论】:

        【解决方案4】:

        您必须删除 ConnectionString 标记中的属性 xdt:Transform。如下所示;

        <connectionStrings>
            <add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
          </connectionStrings>
        

        【讨论】:

        • 但是如果我删除 xdt:Transform,web.config 将不会使用正确的配置..
        猜你喜欢
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        相关资源
        最近更新 更多