{"id":2914,"date":"2010-08-09T11:07:23","date_gmt":"2010-08-09T11:07:23","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2010\/08\/09\/an-example-of-packaging-web-application-containing-database-upgrade-sql-file\/"},"modified":"2022-08-16T01:34:40","modified_gmt":"2022-08-16T08:34:40","slug":"an-example-of-packaging-web-application-containing-database-upgrade-sql-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/an-example-of-packaging-web-application-containing-database-upgrade-sql-file\/","title":{"rendered":"An example of packaging web application containing database upgrade SQL file"},"content":{"rendered":"<p>A few months ago, we have a <a href=\"http:\/\/blogs.msdn.com\/b\/webdevtools\/archive\/2010\/03\/24\/extending-the-web-publishing-pipeline-to-package-database-project-deployed-sql-file.aspx\">blog<\/a> talking about extending the web publishing pipeline to package database project deployed SQL file.&#160; In this blog, I\u2019ll show step by step example of packaging web application with SQL Server database upgrade SQL file using Visual Studio 2010.&#160; This way, we can generate a web package with incremental SQL script to certain database version.&#160; The scenario may help distributions of a web upgrade package which needs to be installed on many different locations, each with their own SQL server database.<\/p>\n<p>&#160;<\/p>\n<p>1. Create a Web Application Project<\/p>\n<p>2. Add a new \u201cSQL Server 2008 Database Project\u201d to the solution<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/1122.image_21_thumb.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image_2[1]\" border=\"0\" alt=\"image_2[1]\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/1122.image_21_thumb.png\" width=\"574\" height=\"241\" \/><\/a> <\/p>\n<p>3. Prepare a development database, such as my test1 database as the following:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/7180.image_thumb-1.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/7180.image_thumb-1.png\" width=\"253\" height=\"411\" \/><\/a> <\/p>\n<p>4. Right click our database project in solution explorer, and select \u201cImport Database Objects and Settings\u201d, then select the development database as connection in the \u201cImport Database Wizard\u201d dialog.&#160; Click Start then finish when import succeeded.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/3364.image_thumb_1-1.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/3364.image_thumb_1-1.png\" width=\"394\" height=\"546\" \/><\/a> <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/0552.image_thumb_2-1.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/0552.image_thumb_2-1.png\" width=\"697\" height=\"536\" \/><\/a> <\/p>\n<\/p>\n<p>5. In database project property page, select \u201cDeploy\u201d tab, and edit the target database settings.&#160; Let\u2019s targeting the development database so that we can update to development database easily through Visual Studio deploy command.&#160; Also, choose the deploy action to be \u201cCreate a deployment script (.sql) and deploy to the database\u201d<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/1134.image_thumb_4-1.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/1134.image_thumb_4-1.png\" width=\"752\" height=\"319\" \/><\/a> <\/p>\n<p>&#160;<\/p>\n<p>6. Run deploy for the test database to check if it builds successfully.&#160; Nothing should be deployed to the development database at this point since they should be identical.&#160; Check the deployed SQL directory %database project dir%sql%configuration% making sure sql file is generated<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/3201.image_thumb_5-1.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/3201.image_thumb_5-1.png\" width=\"769\" height=\"562\" \/><\/a> <\/p>\n<p>7. In Web application project\u2019s property page, tab \u201cPackage\/Publish SQL&#8217;\u201d, add a new database entry for debug configuration.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/3386.image_41_thumb.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image_4[1]\" border=\"0\" alt=\"image_4[1]\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/3386.image_41_thumb.png\" width=\"780\" height=\"256\" \/><\/a> <\/p>\n<p>8. Select it, add the deployed database script (generated in step 6) to the script list<\/p>\n<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/8081.image_thumb_6.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/08\/8081.image_thumb_6.png\" width=\"779\" height=\"439\" \/><\/a> <\/p>\n<p>6. Add extension pipeline project target file &lt;projectName&gt;.wpp.targets file in the same directory as the web project file.&#160; How &lt;projectName&gt;.wpp.targets works is explained in <a href=\"http:\/\/blogs.msdn.com\/webdevtools\/archive\/2010\/02\/09\/how-to-extend-target-file-to-include-registry-settings-for-web-project-package.aspx\">how to package registry blog<\/a>. For example, in the same directory as test1.vbproj file, add test1.wpp.targets file.&#160; Make its content as following :<\/p>\n<pre class=\"code\"><span style=\"color: blue\">&lt;!--<\/span><span style=\"color: green\">********************************************************************<\/span><span style=\"color: blue\">--&gt;\r\n&lt;!-- <\/span><span style=\"color: green\">Task dependent db project custom deploy before build <\/span><span style=\"color: blue\">--&gt;\r\n&lt;!--<\/span><span style=\"color: green\">********************************************************************<\/span><span style=\"color: blue\">--&gt;\r\n&lt;<\/span><span style=\"color: #a31515\">Project <\/span><span style=\"color: red\">xmlns<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">http:\/\/schemas.microsoft.com\/developer\/msbuild\/2003<\/span>&quot;<span style=\"color: blue\">&gt;\r\n  &lt;<\/span><span style=\"color: #a31515\">PropertyGroup<\/span><span style=\"color: blue\">&gt;\r\n      &lt;<\/span><span style=\"color: #a31515\">MyTargetConnectionString <\/span><span style=\"color: red\">Condition<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">'$(MyTargetConnectionString)'==''<\/span>&quot;<span style=\"color: blue\">&gt;<\/span>&quot;Data Source=1p18-fwg36%3BIntegrated Security=True%3BPooling=False&quot;<span style=\"color: blue\">&lt;\/<\/span><span style=\"color: #a31515\">MyTargetConnectionString<\/span><span style=\"color: blue\">&gt;\r\n      &lt;<\/span><span style=\"color: #a31515\">MyTargetDatabase <\/span><span style=\"color: red\">Condition<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">'$(MyTargetDatabase)'==''<\/span>&quot;<span style=\"color: blue\">&gt;<\/span>StageDatabase1<span style=\"color: blue\">&lt;\/<\/span><span style=\"color: #a31515\">MyTargetDatabase<\/span><span style=\"color: blue\">&gt;\r\n\r\n      &lt;<\/span><span style=\"color: #a31515\">AfterAddContentPathToSourceManifest <\/span><span style=\"color: red\">Condition<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">'$(AfterAddContentPathToSourceManifest)'==''<\/span>&quot;<span style=\"color: blue\">&gt;\r\n      <\/span>$(AfterAddContentPathToSourceManifest);\r\n      DeployDbProjBeforePackage;\r\n    <span style=\"color: blue\">&lt;\/<\/span><span style=\"color: #a31515\">AfterAddContentPathToSourceManifest<\/span><span style=\"color: blue\">&gt;\r\n  &lt;\/<\/span><span style=\"color: #a31515\">PropertyGroup<\/span><span style=\"color: blue\">&gt;\r\n\r\n  &lt;!-- <\/span><span style=\"color: green\">Specifies each dependent database project here, its properties will be appended to the target msbuild Properties <\/span><span style=\"color: blue\">--&gt;\r\n  &lt;<\/span><span style=\"color: #a31515\">ItemGroup<\/span><span style=\"color: blue\">&gt;\r\n    &lt;<\/span><span style=\"color: #a31515\">MyDependentDBProjects <\/span><span style=\"color: red\">Include<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">..Database1Database1.dbproj<\/span>&quot;<span style=\"color: blue\">&gt;\r\n      &lt;<\/span><span style=\"color: #a31515\">Properties<\/span><span style=\"color: blue\">&gt;<\/span>DeployScriptFilePath=..Database1sql$(Configuration)Database1.sql<span style=\"color: blue\">&lt;\/<\/span><span style=\"color: #a31515\">Properties<\/span><span style=\"color: blue\">&gt;\r\n    &lt;\/<\/span><span style=\"color: #a31515\">MyDependentDBProjects<\/span><span style=\"color: blue\">&gt;\r\n  &lt;\/<\/span><span style=\"color: #a31515\">ItemGroup<\/span><span style=\"color: blue\">&gt;\r\n\r\n  &lt;<\/span><span style=\"color: #a31515\">Target <\/span><span style=\"color: red\">Name<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">DeployDbProjBeforePackage<\/span>&quot;<span style=\"color: blue\">&gt;\r\n    &lt;!-- <\/span><span style=\"color: green\">Make sure the corresponding directory exists before build the DB projects, especially useful for team build <\/span><span style=\"color: blue\">--&gt;\r\n    &lt;<\/span><span style=\"color: #a31515\">Message <\/span><span style=\"color: red\">Text<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">Creating directory: %(MyDependentDBProjects.rootdir)%(MyDependentDBProjects.directory)sql$(Configuration)<\/span>&quot; <span style=\"color: blue\">\/&gt;\r\n    &lt;<\/span><span style=\"color: #a31515\">MakeDir <\/span><span style=\"color: red\">Directories<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">%(MyDependentDBProjects.rootdir)%(MyDependentDBProjects.directory)sql$(Configuration)<\/span>&quot; <span style=\"color: blue\">\/&gt;\r\n\r\n    &lt;<\/span><span style=\"color: #a31515\">MSBuild <\/span><span style=\"color: red\">Projects<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">@(MyDependentDBProjects)<\/span>&quot; <span style=\"color: red\">Targets<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">Build;Deploy<\/span>&quot; <span style=\"color: red\">Properties<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">Configuration=$(Configuration);TargetConnectionString=$(MyTargetConnectionString);TargetDatabase=$(MyTargetDatabase);DeployToDatabase=False<\/span>&quot;<span style=\"color: blue\">&gt;\r\n    &lt;\/<\/span><span style=\"color: #a31515\">MSBuild<\/span><span style=\"color: blue\">&gt;\r\n  &lt;\/<\/span><span style=\"color: #a31515\">Target<\/span><span style=\"color: blue\">&gt;\r\n\r\n&lt;\/<\/span><span style=\"color: #a31515\">Project<\/span><span style=\"color: blue\">&gt;\r\n<\/span><\/pre>\n<pre class=\"code\">Note:<\/pre>\n<ul>\n<li>\n<pre class=\"code\">connection string needs to be escaped so that semicolon is represented by %3B<\/pre>\n<\/li>\n<li>\n<pre class=\"code\">When msbuild dbproject target, changed its property TargetConnectionString, TargetDatabase, and set DeployToDatabase=False so that it only generate the incremental SQL script<\/pre>\n<\/li>\n<\/ul>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/p>\n<p>7. Save the project and make sure it builds.<\/p>\n<p>8. Test the normal project package from command line, such as:&#160; msbuild test1test1.csproj \/t:package&#160; (Note, if you want to test out in Visual Studio UI, please make sure to reload the project after each change in &lt;projectname&gt;.wpp.targets file.<\/p>\n<p>&#160;<\/p>\n<p>For team build parameters, you can check the previous <a href=\"http:\/\/blogs.msdn.com\/b\/webdevtools\/archive\/2010\/03\/24\/extending-the-web-publishing-pipeline-to-package-database-project-deployed-sql-file.aspx\">blog<\/a>.&#160; To deploy a web package, you can check this <a href=\"http:\/\/blogs.msdn.com\/b\/webdevtools\/archive\/2010\/06\/25\/a-practical-example-of-using-web-application-deployment-package-with-iis7.aspx\">blog<\/a>.&#160; For database deployment, please check this <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd465343.aspx\">MSDN example<\/a>.<\/p>\n<p>&#160;<\/p>\n<p>Xinyang Qiu | Web Platform And Tools<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few months ago, we have a blog talking about extending the web publishing pipeline to package database project deployed SQL file.&#160; In this blog, I\u2019ll show step by step example of packaging web application with SQL Server database upgrade SQL file using Visual Studio 2010.&#160; This way, we can generate a web package with [&hellip;]<\/p>\n","protected":false},"author":404,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[7356,7336,7352,7323,7329],"class_list":["post-2914","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-database-project","tag-msdeploy","tag-package","tag-visual-studio-2010","tag-web-deployment"],"acf":[],"blog_post_summary":"<p>A few months ago, we have a blog talking about extending the web publishing pipeline to package database project deployed SQL file.&#160; In this blog, I\u2019ll show step by step example of packaging web application with SQL Server database upgrade SQL file using Visual Studio 2010.&#160; This way, we can generate a web package with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/404"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=2914"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2914\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=2914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=2914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=2914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}