{"id":3474,"date":"2009-09-28T00:40:22","date_gmt":"2009-09-28T00:40:22","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2009\/09\/28\/dev10-beta1-deploy-web-application-with-sql-express-mdf-file-to-hosts-sql-server\/"},"modified":"2009-09-28T00:40:22","modified_gmt":"2009-09-28T00:40:22","slug":"dev10-beta1-deploy-web-application-with-sql-express-mdf-file-to-hosts-sql-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/dev10-beta1-deploy-web-application-with-sql-express-mdf-file-to-hosts-sql-server\/","title":{"rendered":"Dev10 Beta1, Deploy Web application with SQL Express mdf file to host\u2019s SQL server"},"content":{"rendered":"<p>For many web applications developed using Visual Studio, developers uses SQL express with mdf database file under app_data folder.&#160; If deploying data host requires SQL server, developers have to find ways to deploy the schema and data from mdf file to the host SQL server.<\/p>\n<p>Dev10 Beta1 provides an easy solution to publish the mdf file to the SQL Server host along with other application files.&#160; Here\u2019s a walkthrough.<\/p>\n<p>1. Create a new web application, create a SQL Server Database under app_data folder.&#160; Add some tables to the database, drag a table to the default.aspx designer.&#160; Ctrl-F5 to test it making sure it works.<\/p>\n<p>2. Check the web.config file, note it has a connection string such as following:<\/p>\n<pre class=\"code\"><span style=\"color: blue\">&lt;<\/span><span style=\"color: #a31515\">connectionStrings<\/span><span style=\"color: blue\">&gt;\n    &lt;<\/span><span style=\"color: #a31515\">add <\/span><span style=\"color: red\">name<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">test_EmployeeInfo_SKConnectionString1<\/span>&quot; <span style=\"color: red\">connectionString<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|test_EmployeeInfo_SK.mdf;Integrated Security=True;User Instance=True<\/span>&quot;\n        <span style=\"color: red\">providerName<\/span><span style=\"color: blue\">=<\/span>&quot;<span style=\"color: blue\">System.Data.SqlClient<\/span>&quot; <span style=\"color: blue\">\/&gt;\n&lt;\/<\/span><span style=\"color: #a31515\">connectionStrings<\/span><span style=\"color: blue\">&gt;<\/span><\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/p>\n<p>3. In web application\u2019s property page, Package\/Publish tab, check Exclude files from the app_data folder\u201d so that when packaged, the mdf file itself will not be packaged.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/image_2.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\/2009\/09\/image_thumb.png\" width=\"641\" height=\"317\" \/><\/a> <\/p>\n<p>4. In Deploy-SQL tab, add a new connection, select it and paste the mdf connection string to the \u201cConnection string for the source database\u201d edit box.&#160; Also input the destination\u2019s SQL server connection string, and decide to package schema only or \u201ccomplete database\u201d by checking the corresponding checkboxes.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/image_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\/2009\/09\/image_thumb_2.png\" width=\"634\" height=\"353\" \/><\/a> <\/p>\n<p>5. Refere to <a href=\"http:\/\/blogs.msdn.com\/webdevtools\/archive\/2009\/05\/04\/web-deployment-web-config-transformation.aspx\">Vishal\u2019s post<\/a> for web.config connection string transformation so that packaged web.config file will contain the corresponding deploy time connection string.<\/p>\n<p>6. Save, right click solution explorer project item, choose Package-&gt;\u201cCreate package\u201d context menu item.&#160; Wait for the process end.<\/p>\n<p>7. Now, you\u2019ve created a package contains mdf\u2019s file\u2019s sql script, and without the mdf file under app_data directory.&#160; For debug configuration, default package location is at &lt;project root&gt;objdebugpackageprojectName.zip.<\/p>\n<p>8. You can use the generated &lt;project root&gt;objdebugpackageprojectName_deploy.cmd to test deploy the application and database.<\/p>\n<p>Note:<\/p>\n<p>1.&#160; |DataDirectory| inside connection name can only be used inside VS environment.&#160; Msdeploy dbFullSql provider does not recognize |DataDirectory| in its commandline behavior. <\/p>\n<p>2. In Dev10 Beta1, scripting mdf file has some limitations which may require workaround.&#160; For example, in the connection string, if the expanded AttachDBFilename length is longer than 100 characters, the generated scripts may be in wrong order, and if it is longer than 127 characters, it does not generate at all.&#160; In such time, please add \u201cInitial Catalog=UniqueShortName\u201d to the connection string as a short database logical name.<\/p>\n<p>3. If User Instance is not used in the connection string, if mdf file is opened inside VS, it might be locked to prevent packaging.&#160; In such case, adding User Instance=\u201dtrue\u201d to the connection string, or close the mdf data connection from VS server explorer can do the trick.<\/p>\n<p>4. The above procedures and screen shots are based on Dev10 Beta1, which may be changed in future versions.<\/p>\n<p>Thanks.<\/p>\n<p>Xinyang Qiu \n  <br \/>SDETII <\/p>\n<p>Visual Studio Web Tools<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For many web applications developed using Visual Studio, developers uses SQL express with mdf database file under app_data folder.&#160; If deploying data host requires SQL server, developers have to find ways to deploy the schema and data from mdf file to the host SQL server. Dev10 Beta1 provides an easy solution to publish the mdf [&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":[7341,7323,7329],"class_list":["post-3474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-database","tag-visual-studio-2010","tag-web-deployment"],"acf":[],"blog_post_summary":"<p>For many web applications developed using Visual Studio, developers uses SQL express with mdf database file under app_data folder.&#160; If deploying data host requires SQL server, developers have to find ways to deploy the schema and data from mdf file to the host SQL server. Dev10 Beta1 provides an easy solution to publish the mdf [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3474","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=3474"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3474\/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=3474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=3474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=3474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}