英文:
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=&quot;data source=server;initial catalog=Something;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework&quot;"
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=&quot;data source=server;initial catalog=Something_Dev;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework&quot;"
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=&quot;data source=server;initial catalog=Something;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework&quot;"
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=&quot;data source=server;initial catalog=Something_Dev;persist security info=True;multipleactiveresultsets=True;application name=EntityFramework&quot;"
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:
<connectionStrings>
<add name="MyConnectionString"
connectionString="Debug_Connection_String_Here"
providerName="System.Data.SqlClient" />
</connectionStrings>
and
<connectionStrings>
<add name="MyConnectionString"
connectionString="Production_Connection_String_Here"
providerName="System.Data.SqlClient" />
</connectionStrings>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论