我在不同环境的项目中做了几个配置,一个是调试,另一个是发布
的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="data source=192.168.7.131;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=debugMe;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=192.168.7.131;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=debugMe;MultipleActiveResultSets=True;App=EntityFramework"" 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="data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
问题在于:
每当我使用debug或release config时,连接字符串仍然是web.config中的连接字符串,而不是web.release.config中的连接字符串
我做错了什么?
答案 0 :(得分:2)
有时最好使用逐项转换。而不是部分替换使用
<add name="InvitationEntity" connectionString="metadata=res://*/Models.InvitationSystem.csdl|res://*/Models.InvitationSystem.ssdl|res://*/Models.InvitationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
当节点中有许多属性并且只需要更改一个属性时,使用此方法非常有用,例如,您只需使用SetAttributes而不是Replace。
答案 1 :(得分:2)
您的发布版本设置是否使用Web.live.config文件?默认情况下,Release需要一个名为Web.Release.Config的文件,我只是提到这个,因为该文件被列为Web.live.config,但你正在调用它&#34; Release&#34;。如果它只是一个拼写错误并且名称是一致的,或者您的发布版本配置设置为&#34; live&#34;然后忽略这一点。
答案 2 :(得分:1)
对我来说,转到转换文件的属性并更改属性; Copy to Output Directory
到Copy Always
就行了。得到了Matt Kogaj's answer
答案 3 :(得分:0)
您必须在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="data source=192.168.7.133;initial catalog=Invitation_Debug;persist security info=True;user id=invitation;password=Invi#2014;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>