简体   繁体   中英

App.config transformation is not working based on environment

I have a console application and I need to separate app.config for diff. env in order to use different connection strings.

I right-clicked on App.config -> Add config transforms and got two files:

App.Debug.config

App.Release.config

Those files are following:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document- 
Transform">
<connectionStrings>
<add name="SqlCpaConnectionString" 
     connectionString="blahblah" 
     providerName="System.Data.SqlClient"/>
<add name="AzureStorageConnectionString"
     connectionString="blahblah"
     providerName="Microsoft.WindowsAzure.Storage"/>

When getting the settings like:

ConfigurationManager.ConnectionStrings["AzureStorageConnectionString"].ConnectionString

I get null ref ex on both of envs...

See following answer. This will also work with console apps: How do I use Web.Config transform on my connection strings?

Set the connection string transform:

<connectionStrings>
    <add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword" 
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM