11

A feature requires adding a new connection string. Which should be the way to do this in Helix?

13

You would need to use an XmlTransform to do this. If you look at the Habitat demo site you can see examples of how to do this.

Creating the transform file

You would add your connection string to a ConnectionStrings.config.transform file - this can live in your project in the App_Config folder.

The file is a standard config transform file. Example:

<?xml version="1.0"?>
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="MyDB" 
        connectionString="value for the deployed Web.config file" 
        xdt:Transform="Insert" />
</connectionStrings>

Merging the transform

If you are using the gulp file from the Habitat example, there is already a task that will apply the transforms in the solution:

04-Apply-Xml-Transform

If you are not using that script, you can do the same thing. It calls MSBuild with an applytransform.targets file.

The targets file contains:

<Project ToolsVersion="14.0" DefaultTargets="ApplyTransform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(MSBuildToolsVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="ApplyTransform">
    <ItemGroup>
      <Transform Include="$(TransformFile)" />

      <ConfigsToTransform Include="$(FileToTransform)" Condition="Exists(@(Transform))">
        <TransformPath>%(Transform.Identity)</TransformPath>
      </ConfigsToTransform>
    </ItemGroup>
    <Message Text="@(ConfigsToTransform)"></Message>
    <Message Text="@(Transform)"></Message>

    <TransformXml Source="$(WebConfigToTransform)\%(ConfigsToTransform.Identity)"
                  Transform="%(ConfigsToTransform.TransformPath)"
                  Destination="$(WebConfigToTransform)\%(ConfigsToTransform.Identity)"
                  Condition="Exists(@(Transform))"/>
  </Target>
</Project>

So you can use the MSBuild command line to use that:

msbuild applytransforms.xml /t:ApplyTransforms

More information about how to set this up is here:

  • Beat me to it! ;) – Zachary Kniebel Oct 31 '16 at 16:48
  • Note that for connection strings the <configuration> node must be removed. Thanks :) – JGonDev Nov 1 '16 at 8:52
  • Yes - that is because the ConnectionStrings.config lives outside the main web.config. I'll update the answer. – Richard Seal Nov 1 '16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.