How to: Deploy Changes using Team Foundation Build

You can build and deploy your database in Visual Studio by using MSBuild or Team Foundation Build. If you want to build and deploy your database project in a build lab environment, you would use Team Foundation Build. To use Team Foundation Build, you must have a computer on which you have it installed. For more information, see Team Foundation Build Overview.

The process to deploy changes by using Team Foundation Build is as follows:

  • Create a build definition.

  • Modify the build project file manually to override the standard AfterDropBuild target.

  • Modify the database project file manually to specify the target database and connection.

  • Check in the updated files.

  • Use the new build definition to deploy your database project.

Before you can deploy changes using Team Foundation Build, you must:

  • Have a database project with its properties set.

  • Check the database project into version control.

Create a Build Definition

To create a build definition

  1. Open Visual Studio with Team Explorer.

  2. Open the solution that contains the database project that you want to deploy.

  3. Create a build definition. For more information, see How to: Create a Build Definition. When you create the build definition, specify the current solution to be built. If you have not created a custom project configuration, click the default configuration.

    Next you will modify the BuildDefinition.proj file.

Modify the Build Project File

To deploy the database project, you must override the AfterDropBuild target with the SqlDeploy task.

To modify the build project file

  1. Check out the BuildDefinition.proj file from version control. If you are using Team Foundation version control, open Source Control Explorer, expand the team project, and expand the TeamBuildTypes folder to find the build project files.

  2. Open the BuildDefinition.proj file in the editor of your choice.

  3. At the bottom of the file, between the </ItemGroup> element and the </Project> element, add the following:

    <Target Name="AfterDropBuild">
      <MSBuild Projects="$(SolutionRoot)\SolutionName\ProjectName\ProjectName.dbproj" Properties="Configuration=Default;OutDir=$(SolutionRoot)\..\binaries\Default\" Targets="Deploy" />
    </Target>
    

    Replace SolutionName with the name of the solution that contains the database project, and replace ProjectName with the name of the database project. If you are not using the Default configuration for your database project, replace Default with the name of the appropriate project configuration both in Configuration and in OutDir.

    Team Foundation Build copies files to a drop location that is usually a network share. If you want to deploy the database from that location instead of from the build computer, you would instead use the following:

    <Target Name="AfterDropBuild">
      <MSBuild Projects="$(SolutionRoot)\SolutionName\ProjectName\ ProjectName.dbproj" Properties="Configuration=Release;OutDir=$(DropLocation)\$(BuildNumber)\Release\" Targets="Deploy"/>
    </Target>
    
  4. Save the changes to BuildDefinition.Proj.

Modify the Database Project File

The target connection and database are stored in the ProjectName.dbproj.user file, which is user specific and not typically checked in to version control. You require those settings to deploy your database. Therefore, you must modify the ProjectName.dbproj file manually to specify the target connection and database.

To modify the database project file

  1. Check the ProjectName.dbproj file out from your version control system.

  2. On the File menu, click Close Solution. This step is necessary because you cannot modify a project file that is in an open solution.

  3. On the File menu, point to Open, and click File.

  4. Browse to the folder that contains the database project file (ProjectName.dbproj), and double-click ProjectName.dbproj.user.

  5. Copy the lines that contain the definitions for the TargetDatabase and TargetConnectionString properties from the section in the ProjectName.dbproj.user file for the configuration that you want to build. These lines will resemble the following:

    <TargetDatabase>MyTargetDatabaseName</TargetDatabase>
    <TargetConnectionString>Data Source=ServerName\InstanceName;Integrated Security=True;Pooling=False</TargetConnectionString>
    

    Note

    The name of the database project is used if you do not specify the name of a target database.

  6. Copy those two entries to the Clipboard.

  7. On the File menu, point to Open, and click File.

  8. Browse to the folder that contains the database project file (ProjectName.dbproj), and double-click ProjectName.dbproj.

  9. Scroll down to the PropertyGroup for the configuration that you want to deploy using Team Foundation Build.

  10. Paste the entries that you copied to the Clipboard into the project file. If TargetDatabase and TargetConnectionString already contain empty elements, you should overwrite those entries.

  11. On the File menu, click Save ProjectName**.dbproj**.

Check in the Updated Files

Team Foundation Build uses the version of the database project that is checked into version control. Before you use that version to deploy your database, you must check in the changes that you made to the build project and the database project file.

To check in the updated files

  1. On the File menu, point to Source Control, and click Check In Pending Changes.

  2. Follow the instructions for your version control system to complete the check in for the build project and the database project file.

Deploy your Database

You have now configured the build definition and set up the database project for use with Team Foundation Build. You can perform a test build to verify whether your database deploys successfully.

To deploy your database

  1. In Team Explorer, expand the Team Builds folder, right-click the build definition that you created to deploy the database project, and click Queue Build TeamProjectName.

    The Build dialog box appears.

  2. Confirm that the values in the Build definition, Build agent, and Drop folder for this Build fields are all appropriate, and then click Queue.

    The Queued tab of Build Explorer appears. For more information, see Understanding the Team Foundation Build Explorer.

See Also

Tasks

How to: Configure Build Settings for Database and Server Projects

How to: Configure Deployment Settings for Database and Server Projects

How to: Run Database Unit Tests from Team Foundation Build

Concepts

Build and Deploy Databases to an Isolated Development Environment

Team Foundation Build Overview

An Overview of Database Build and Deployment

Terminology Overview of Database Edition