Share via


Pre and Post Deployment Events

Visual Studio makes it relatively easy to hook in to pre and post build events, by using the Project properties tab named Build Events. However when using Database Projects (.dbproj) you more often need to do something at deployment time. The good news is that the standard MSBuild framework, already defines these events, the bad news is that you manually have to update the project file yourself.

The steps are pretty simple:

  • Unload the project, by right clicking on the project node inside Solution Explorer and selecting the “Unload Project” option
  • This will grey-out the project node inside Solution Explorer and mark it (unavailable). Now you can right click on it again and select the “Edit <your project file name>.dbproj” option. This will load the project file inside the XML editor.
  • Next you have to add the target elements for the PreDeployEvent and PostDeployEvent, like below:
    1:  <Target Name="PreDeployEvent">
    2:      <Message Importance="high" Text="Pre deployment event"/>
    3:  </Target>
    4:   
    5:  <Target Name="PostDeployEvent" >
    6:      <Message Importance="high" Text="Post deployment event"/>
    7:  </Target>

 

  • After you made all the changes, you save and close the project file and reload the project, by right clicking on the project node again and choosing the “Reload Project” option.
    • If you forgot to close the editor, you will be asked if it can be closed and saved. Only after you confirmed this is OK, the project will be reloaded.
  • Now you can test if your pre and post-deployment events are firing by deploying your project. If everything works you should see the pre and post build event messages in the output window like on lines 9 and 14.
    1:  ------ Build started: Project: nw, Configuration: Debug Any CPU ------
    2:      Loading project references...
    3:      Loading project files...
    4:      Building the project model and resolving object interdependencies...
    5:      Validating the project model...
    6:      Writing model to nw.dbschema...
    7:    nw -> D:\demo\nw\sql\debug\nw.dbschema
    8:  ------ Deploy started: Project: nw, Configuration: Debug Any CPU ------
    9:    Pre deployment event
   10:      Deployment script generated to:
   11:  D:\demo\nw\sql\debug\nw.sql
   12:   
   13:      The deployment script was generated, but was not deployed. You can change the deploy action on the Deploy tab of the project properties.
   14:    Post deployment event
   15:  ========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
   16:  ========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========

Now you should be able to hook-up your events to the pre and post deployment events. Please keep in mind that pre-deployment scripts do not change the outcome of deployments like discussed earlier in this blog post.

Success,

GertD @ DBProj.com

Comments

  • Anonymous
    November 05, 2009
    thanks for this. i've successfully used this to kick off SSIS packages as post-deploy events, to update data after every deployment.