Publish an ASP.NET Core Web App With EF Migrations to Azure AppService

ASP.NET core app with database running on Azure:

http://aspnetcorebloglist.azurewebsites.net/

Github source:

https://github.com/dotnetpublish/AspNetCoreBlogList

Requirements:

You will need Visual Studio 2015 Update 3 and then download the .NET Core for Visual Studio official MSI Installer. If you don’t have Visual Studio already, you can download Visual Studio Community 2015 for free. Download the latest Azure SDK 2.9.1 for the Azure tooling features.

Step 1: Create an ASP.NET Core Web app with Database.

For this blog, we will be using the sample app – AspNetCoreBlogPosts from github.

git clone https://github.com/dotnetpublish/AspNetCoreBlogList

 

Step 2: Run the application locally on IIS Express

Right click on the web app, set as the startup project and click F5 to run the web app locally on IIS express. This should open the web app in the browser.Verify the app is running fine.

Step 3: Create an AppService with SQL Server on Microsoft Azure

  • Right click on the project, click publish & Select Microsoft Azure App Service

Publish_AzureAppService

  • Click New to create an Azure App Service

New_AzureAppService

  • Provide the following details
    • Unique name for the web app
    • Create/select a resource group (Collection of Assets to run an application)
    • Create/Select an app service plan(Learn more about app service plan)

New_AzureAppService_rg_sp

  • Click on Services tab and click Add for creating a SQL Server database.

Add_Sql_Server.png

  • Provide a sql server name(either create a new one or select an existing one) , provide user id and password to create the server and database.
    • Connection string with the name ‘DefaultConnection’ (or any other name that is provided in the textbox) will be added to Azure app Service during provisioning.

Selectdb

  • Click Ok and go back to the hosting tab.  Hosting tab will show the summary of all Azure assets that will be created.
  • Click Create.

Step 4: Publish the app to Azure App Service with EF Migrations.

  • Once the provisioning the complete, it will go to the Connection tab in the Publish dialog.Click Validate connection to verify the connection.

 

ConnectionTab

  • Click Next to go to the Settings tab
    • Check remove additional files at Destination.
    • Expand the database section, Check ‘Use this connection string at destination’ for both the connections string entries. This means that during the publish operation, appSettings.<EnvironmentName>.Json (Environment name can be defined as a publish property) will be created/updated(if already present).
    • The connection string is auto-populated for the database created in Azure in the previous step.

Check_database_Section

  • Expand  the Entity Framework migrations section
    • Check the checkboxes to apply the migrations for ApplicationDBContext and BlogPostContext. When this is checked, during the publish operation, a sql script is generated and run against the target database with the provided connection string.
    • The connection string used for EF migration could be different than the above. The EF migration could be run as admin and application could use a non-admin connection string if needed.

Check_EF

  • Next and File Preview

file_preview

  • Double click the file in the preview pane to view the file diff..

file_diff.png

Click ‘Preview Migration’ for the Context to view the script that will be applied during publish.

Preview_EF_Migration

EF_Migration_Script.png

  • Click Publish.
    • On Publish, the db migration is applied and tables are created.
    • appsettings.production.json is created and updated with the selected connection string.
{ 
"ConnectionStrings": {
 "DefaultConnection": "Data Source=tcp:dotnetpublish.database.windows.net,1433;Initial Catalog=AspNetCoreBlogList_db;User Id=<removed from blog>;Password=<removed from blog>",
 "BlogConnection": "Data Source=tcp:dotnetpublish.database.windows.net,1433;Initial Catalog=AspNetCoreBlogList_db;User Id=<removed from blog>;Password=<removed from blog>"
 }}

Sample ASP.NET Core Web app running on Azure App Service

http://aspnetcorebloglist.azurewebsites.net/

app_In_Production

 

Author: vijayrkn

Developer at Microsoft focusing on Cloud & .NET Development.

2 thoughts on “Publish an ASP.NET Core Web App With EF Migrations to Azure AppService”

Leave a comment