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.
To create a build definition
Open Visual Studio with Team Explorer.
Open the solution that contains the database project that you want to deploy.
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.
To deploy the database project, you must override the AfterDropBuild target with the SqlDeploy task.
To modify the build project file
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.
Open the BuildDefinition.proj file in the editor of your choice.
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>
Save the changes to BuildDefinition.Proj.
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
Check the ProjectName.dbproj file out from your version control system.
On the File menu, click Close Solution. This step is necessary because you cannot modify a project file that is in an open solution.
On the File menu, point to Open, and click File.
Browse to the folder that contains the database project file (ProjectName.dbproj), and double-click ProjectName.dbproj.user.
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>
The name of the database project is used if you do not specify the name of a target database.
Copy those two entries to the Clipboard.
On the File menu, point to Open, and click File.
Browse to the folder that contains the database project file (ProjectName.dbproj), and double-click ProjectName.dbproj.
Scroll down to the PropertyGroup for the configuration that you want to deploy using Team Foundation Build.
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.
On the File menu, click Save ProjectName.dbproj.
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
On the File menu, point to Source Control, and click Check In Pending Changes.
Follow the instructions for your version control system to complete the check in for the build project and the database project file.
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
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.
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.