{"id":24017,"date":"2018-05-18T19:40:05","date_gmt":"2018-05-18T19:40:05","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/?p=13905"},"modified":"2018-05-18T19:40:05","modified_gmt":"2018-05-18T19:40:05","slug":"exploring-azure-app-service-web-apps-and-sql-azure","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2018\/05\/18\/exploring-azure-app-service-web-apps-and-sql-azure\/","title":{"rendered":"Exploring Azure App Service \u2013 Web Apps and SQL Azure"},"content":{"rendered":"<p>There is a good chance that your web app uses a database. In <a href=\"https:\/\/blogs.msdn.microsoft.com\/webdev\/2018\/05\/02\/exploring-azure-app-service-introduction\/\" target=\"_blank\">my previous post introducing Azure App Service<\/a>, I showed some of the benefits of hosting apps in Azure App Service, and how easy it is to get a basic site running in a few clicks. In this post I\u2019ll show how to set up a SQL Azure database along with an App Service Web App from Visual Studio, and apply Entity Framework automatically as part of publish.<\/p>\n<h3>Let\u2019s get going<\/h3>\n<p>To get started, you\u2019ll first need:<\/p>\n<ul>\n<li>Visual Studio 2017 with the <em>ASP.NET and web development<\/em> workload installed (<a href=\"https:\/\/www.visualstudio.com\/thank-you-downloading-visual-studio\/?sku=Community&amp;rel=15&amp;utm_source=azuremscom&amp;utm_medium=clickbutton&amp;utm_campaign=tailored_azuredlpage&amp;rid=34348\">download now<\/a>)<\/li>\n<li>An Azure account:\n<\/li>\n<li>Any ASP.NET or ASP.NET Core app that uses a SQL Database. For the purposes of this post, I\u2019ll create a new ASP.NET Core app with Individual Authentication:\n<ul>\n<li>On the \u201cNew ASP.NET Core Web Application\u201d dialog, click the \u201cChange Authentication\u201d button.<br \/><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image00220.jpg\"><img decoding=\"async\" title=\"clip_image002\" alt=\"clip_image002\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure.jpg\" width=\"650\" border=\"0\" \/><\/a><\/li>\n<\/ul>\n<\/li>\n<li>Then select the \u201cIndividual User Accounts\u201d radio button and click \u201cOK\u201d.<\/li>\n<li>Click OK.<\/li>\n<\/ul>\n<p>I can now run my project locally (F5) and create user accounts which will be stored in a SQL Server Express Local DB on my machine.<\/p>\n<h3>Publishing to App Service with a Database<\/h3>\n<p>Let\u2019s publish our application to Azure. To do this, I\u2019ll right click my project in Solution Explorer and choose \u201cPublish\u201d<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0034.png\"><img decoding=\"async\" title=\"clip_image003\" alt=\"clip_image003\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure.png\" width=\"400\" border=\"0\" \/><\/a><\/p>\n<p>This brings up the Visual Studio publish target dialog, which will default to the Azure App Service pane with the \u201cCreate new\u201d radio button selected. To continue click \u201cPublish\u201d.<\/p>\n<p>This brings up the \u201cCreate App Service\u201d dialog (see the <a href=\"https:\/\/blogs.msdn.microsoft.com\/webdev\/2018\/05\/02\/exploring-azure-app-service-introduction\/#appservice_concepts\" target=\"_blank\">\u201cKey App Service Concepts\u201d section of my previous post for an explanation of the fields<\/a>). To create a SQL Database for our app to use, click the \u201cCreate a SQL Database\u201d link in the top right section of the dialog.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0055.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image005\" alt=\"clip_image005\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure-1.jpg\" width=\"644\" height=\"222\" border=\"0\" \/><\/a><\/p>\n<p>This will bring up the \u201cConfigure SQL Database\u201d dialog.<\/p>\n<ul>\n<li><strong>Note:<\/strong> If you are using a Visual Studio Enterprise subscription, many regions will not let you create a SQL Azure database so I recommend choosing \u201cEast US\u201d or \u201cWest US 2\u201d depending on where you are located (we are adding logic in in the Visual Studio 2017 15.8 update to remove those regions if that\u2019s the case, but for now you\u2019ll need to choose an appropriate region). To do this, click the \u201cNew\u2026\u201d button next to your \u201cHosting Plan Dropdown\u201d and pick the appropriate region (\u201cEast US\u201d or \u201cWest US 2\u201d).<\/li>\n<li>Since I don\u2019t have an existing SQL Server, the first thing I need to do is create a server to host the database, so I\u2019ll click the \u201cNew\u2026\u201d button next to the \u201cSQL Server\u201d dropdown,<\/li>\n<li>Choose a location for the database.<\/li>\n<li>Provide an administrator user name and password for the server<\/li>\n<li>Click \u201cOK\u201d<br \/><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0073.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image007\" alt=\"clip_image007\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure-2.jpg\" width=\"431\" height=\"484\" border=\"0\" \/><\/a><\/li>\n<li>Make sure the connection string name field matches the name of the connection string your application uses to access the database (if using a new project, it is \u201cDefaultConnection\u201d which will be prepopulated for you).<br \/><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0094.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image009\" alt=\"clip_image009\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure-3.jpg\" width=\"430\" height=\"484\" border=\"0\" \/><\/a><\/li>\n<li>Click OK<\/li>\n<li>Then click the \u201cCreate\u201d button on the \u201cCreate App Service\u201d dialog<\/li>\n<\/ul>\n<p>It should take ~2-3 minutes to create all of the resources in Azure, then your application will publish and a browser will open to your home page.<\/p>\n<h3>Configuring EF Migrations<\/h3>\n<p>At this point there is a database for your app to use in the cloud, but EF migrations have not been applied, so any functionality that relies on the database (e.g. Registering for a user account) will result in an error.<\/p>\n<p>To apply EF migrations to the database:<\/p>\n<ul>\n<li>Click the \u201cConfigure\u2026\u201d button on the publish summary page<br \/><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0114.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image011\" alt=\"clip_image011\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure-4.jpg\" width=\"644\" height=\"336\" border=\"0\" \/><\/a><\/li>\n<li>Navigate to the \u201cSettings\u201d tab<\/li>\n<li>When it finishes discovering data contexts, expand the \u201cEntity Framework Migrations\u201d section, and check the \u201cApply this migration on publish\u201d for all of the contexts it finds<br \/><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0133.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image013\" alt=\"clip_image013\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure-5.jpg\" width=\"610\" height=\"484\" border=\"0\" \/><\/a><\/li>\n<li>Click \u201cSave\u201d<\/li>\n<li>Click Publish again, in the output window you should see \u201cGenerating Entity framework SQL Scripts\u201d and then \u201cGenerating Entity framework SQL Scripts completed successfully\u201d<br \/><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/2018\/05\/clip_image0154.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image015\" alt=\"clip_image015\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/exploring-azure-app-service-web-apps-and-sql-azure-6.jpg\" width=\"644\" height=\"381\" border=\"0\" \/><\/a><\/li>\n<\/ul>\n<p>That\u2019s it, your web app and SQL Azure database are both configured and running in the cloud.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hopefully, this post showed you how easy it is to try App Service and SQL Azure. We believe that for most people, App Service is the easiest place to get started with cloud development, even if you need to move to other services in the future for further capabilities (<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/app-service\/choose-web-site-cloud-service-vm\">compare hosting options<\/a>). As always, let us know if you run into any issues, or have any questions below or <a href=\"https:\/\/twitter.com\/AndrewBrianHall\">via Twitter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a good chance that your web app uses a database. In my previous post introducing Azure App Service, I showed some of the benefits of hosting apps in Azure App Service, and how easy it is to get a basic site running in a few clicks. In this post I\u2019ll show how to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":24018,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[80,67,54,81],"class_list":["post-24017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-webdev","tag-aspnet","tag-aspnetcore","tag-azure","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/24017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=24017"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/24017\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/24018"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=24017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=24017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=24017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}