Using the Configuration Manager in Visual Studio setup the appropriate configurations e.g. add a Test configuration. Debug goes to dev, Test goes to test, Release goes to production. Test and Release should have the same build configuration (if you want test environment code same as prod env code) but will have different config transforms applied (e.g. the connection strings for dbs will be different).
In Visual Studio go to the Build tab on the project properties and configure each configuration e.g. make the test configuration the same as the release configuration
Add any additional web.config transform files that are needed. For a web.config you can right click. For an app.config project add each file manually, then unload the project file and edit it. Make each of the transforms dependent on the app.config file.
Update the transform configs to have environment specific values; the application of these are controlled by the transform attributes. Only include elements that need to be changed. Items in transform will be merged into the original config i.e. you don't have to repeat data in here that isn't changing
In TFS config transforms will not happen automatically. To get this to work we manually add a step into the project file. This won't effect local builds. In Visual Studio unload the project. Edit the project file and add the transform step - it differs for app.config and web.config so add appropriately.
Web.config
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="ApplyTransform" Condition="Exists('Web.$(Configuration).config')">
<TransformXml Source="web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
<Target Name="BeforeBuild">
<Exec Command="attrib -r Web.config" />
<CallTarget Targets="ApplyTransform" />
</Target>
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('app.$(Configuration).config')">
<!-- Generate transformed app config in the intermediate directory -->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
Create a build for the solution in TFS. There is a variable on the build called BuildConfiguration - every time this build is run it will compile the code using that configuration. E.g. if it is set to debug then the debug code will be what is released (i.e. it will go to dev, test and prod :( ).
To make TFS build all configurations from the same code set update the BuildConfiguration to be debug,test,release (a comma delimited list)
On the Options tab select the 'Multi-configuration' option, and in Multipliers enter the value BuildConfiguration
On the Copy Files action make the target folder contain the value of the BuildConfiguration (each in their own dir)
On the Publish Build Artifacts action but the BuildConfiguration value into the artifact name
When the build is run it will do a build for all three configurations
Three properly named artifacts will be created
On the release each environment has a file copy action with the source (and target) being different for each environment. This will copy everything. This could be better.
Sources:
http://stackoverflow.com/questions/3004210/app-config-transformation-for-projects-which-are-not-web-projects-in-visual-stud
http://stackoverflow.com/questions/8082662/how-to-select-different-app-config-for-several-build-configurations
https://codepunk.io/team-foundation-server-build-and-release-with-web-config-transforms/