如何在 web.config 中使用转换,以使调试连接字符串与生产环境不同

huangapple go评论101阅读模式
英文:

How to use Transforms on web.config so debug connection string is different than production

问题

以下是使用 transforms 来替换连接字符串以实现在生产环境和调试环境使用不同连接字符串的示例:

在生产模式下,你可以使用以下连接字符串:

<add name="SomethingEntities" 
connectionString="metadata=res://*/DataModel.SomethingEntity.csdl|res://*/DataModel.SomethingEntity.ssdl|res://*/DataModel.SomethingEntity.msl;provider=System.Data.SqlClient;provider connection string="data source=server;initial catalog=Something;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework"" 
providerName="System.Data.EntityClient"/>

在调试模式下,你可以使用以下连接字符串:

<add name="Something_Dev_Entities" 
connectionString="metadata=res://*/DataModel.Something_Dev_Entity.csdl|res://*/DataModel.Something_Dev_Entity.ssdl|res://*/DataModel.Something_Dev_Entity.msl;provider=System.Data.SqlClient;provider connection string="data source=server;initial catalog=Something_Dev;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework"" 
providerName="System.Data.EntityClient"/>

这样,你可以在生产和调试环境中分别使用不同的连接字符串。

英文:

Can i get an example of how I would use transforms to replace my connection string so that production has the following connection string, and debug has the second one?

<add name="SomethingEntities" 
		 
connectionString="metadata=res://*/DataModel.SomethingEntity.csdl|res://*/DataModel.SomethingEntity.ssdl|res://*/DataModel.SomethingEntity.msl;provider=System.Data.SqlClient;provider connection string="data source=server;initial catalog=Something;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework"" 
		 providerName="System.Data.EntityClient"/>

In debug mode, I'd like my connectionstring to be

<add name="Something_Dev_Entities" 
		 connectionString="metadata=res://*/DataModel.Something_Dev_Entity.csdl|res://*/DataModel.Something_Dev_Entity.ssdl|res://*/DataModel.Something_Dev_Entity.msl;provider=System.Data.SqlClient;provider connection string="data source=server;initial catalog=Something_Dev;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework"" 
		 providerName="System.Data.EntityClient"/>```


</details>


# 答案1
**得分**: 1

所以,你应该创建配置转换以管理不同的设置。

例如,你可以创建两个新的配置文件:

- **Web.Debug.config**:这是用于调试环境的转换文件。
- **Web.Release.config**:这是用于生产环境的转换文件。

对于每个新文件,你应该像这样创建自定义配置:

```xml
<connectionStrings>
  <add name="MyConnectionString"
       connectionString="Debug_Connection_String_Here"
       providerName="System.Data.SqlClient" />
</connectionStrings>

<connectionStrings>
  <add name="MyConnectionString"
       connectionString="Production_Connection_String_Here"
       providerName="System.Data.SqlClient" />
</connectionStrings>

当你发布应用程序时,在Visual Studio中选择适当的构建配置(Debug或Release)。转换将在发布过程中应用,Web.config文件中的连接字符串将被替换为所选环境的正确连接字符串。

英文:

So, you Should create a configuration transforms to manage different settings.

for example, you can create two new config files:

  • Web.Debug.config: This is the transform file for the debug environment.
  • Web.Release.config: This is the transform file for the production environment.

for each new files you should create your custom configuration like this:

&lt;connectionStrings&gt;
  &lt;add name=&quot;MyConnectionString&quot;
       connectionString=&quot;Debug_Connection_String_Here&quot;
       providerName=&quot;System.Data.SqlClient&quot; /&gt;
&lt;/connectionStrings&gt;

and

&lt;connectionStrings&gt;
  &lt;add name=&quot;MyConnectionString&quot;
       connectionString=&quot;Production_Connection_String_Here&quot;
       providerName=&quot;System.Data.SqlClient&quot; /&gt;
&lt;/connectionStrings&gt;

When you publish your application, select the appropriate build configuration (Debug or Release) in Visual Studio. The transformation will be applied during the publishing process, and the connection string in the Web.config file will be replaced with the correct one for the selected environment.

huangapple
  • 本文由 发表于 2023年8月10日 23:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定