Tutorial: Deploy an ASP.NET Core and Azure SQL Database app to Azure App Service

In this tutorial, you'll learn how to deploy an ASP.NET Core app to Azure App Service and connect to an Azure SQL Database. Azure App Service is a highly scalable, self-patching, web-hosting service that can easily deploy apps on Windows or Linux. Although this tutorial uses an ASP.NET Core 6.0 app, the process is the same for other versions of ASP.NET Core and ASP.NET Framework.

This article assumes you're familiar with .NET and have it installed locally. You'll also need an Azure account with an active subscription. If you don't have an Azure account, you can create one for free.

1 - Set up the Sample Application

To follow along with this tutorial, Download the Sample Project from the repository https://github.com/Azure-Samples/msdocs-app-service-sqldb-dotnetcore or clone it using the Git command below:

git clone https://github.com/Azure-Samples/msdocs-app-service-sqldb-dotnetcore.git
cd msdocs-app-service-sqldb-dotnetcore

This is an architecture diagram about how the solution works in Azure

2 - Create the App Service

Let's first create the Azure App Service that hosts our deployed Web App. There are several different ways to create an App Service depending on your ideal workflow.

Sign in to the Azure portal and follow these steps to create your Azure App Service resources:

Instructions Screenshot
In the Azure portal:
  1. Enter App Service in the search bar at the top of the Azure portal and select the App Services item that appears in the results.
A screenshot showing how to use the search box in the top tool bar to find App Services in Azure.
On the App Services page, select + Create A screenshot showing the create button on the App Services page used to create a new web app.
On the Create Web App page, fill out the form as follows.
  1. Resource Group → Select Create new and enter a name of msdocs-core-sql.
  2. Name → Enter a unique name of your choice, such as coreSqlXYZ, where XYZ are letters or numbers of your choosing. This App Service name must be unique across Azure.
  3. PublishCode
  4. Runtime stack.NET 6.0
  5. Operating SystemWindows
  6. Region → Any Azure region near you.
  7. App Service Plan → Select Create New and give your App Service plan a name of MyAppServicePlan.
  8. Select Change size under Sku and size settings to select the App Service plan specifications.
A screenshot showing the form to fill out to create a web app in Azure.
The App Service plan controls what resources (CPU/memory) are available to your app and the cost of those resources. You can learn more about choosing an App Service plan in the article App Service plan overview.
For this example, select Dev / Test at the top of the screen and select the F1 (Free) plan.
When finished, select Apply to apply your changes.
A screenshot of the Spec Picker dialog that lets you select the App Service plan to use for your web app.
On the main Create Web App page, select the Review + create button at the bottom of the screen.

On the Review page, select Create to create your App Service. The deployment of this new App Service may take a few minutes to complete.
A screenshot of the main web app create page showing the button to select on to create your web app in Azure.

3 - Create the Database

Next, let's create the Azure SQL Database that manages the data in our app.

Sign in to the Azure portal and follow these steps to create your Azure App Service resources:

Instructions Screenshot
In the Azure portal:
  1. In the search bar at the top of the Azure portal, enter SQL. Select the item labeled SQL Servers from the search results.
A screenshot showing how to use the search box in the top tool bar to find Azure SQL in Azure.
On the SQL Servers page, select + Create. A screenshot showing the create button on the SQL Servers page used to create a new database server.
On the Create Server page, fill out the form as follows.
  1. Resource Group - choose the msdocs-core-sql group you created.

  2. Server name - enter a globally unique name such as coredbserverXYZ where XYZ are random numbers.

  3. Location - select a region near you.

  4. Authentication method - select Use SQL Authentication.

  5. Server admin login - enter a username of your choice.

  6. Password - enter a password you'll remember.

A screenshot showing the form to fill out to create a SQL Server in Azure.
After your SQL Server has been provisioned, in the search bar at the top of the Azure portal, enter SQL. Select the item labeled SQL Databases from the search results. A screenshot showing how to use the search box to find the SQL databases item in Azure.
On the SQL Databases page, select + Create. A screenshot showing the create button in on the SQL databases page.
On the Create Database page, fill out the form as follows.
  1. Resource Group - choose the msdocs-core-sql group you created earlier.

  2. Database name - enter a value of coreDb.

  3. Server - select the coredbserverXYZ you created earlier.

  4. Leave the rest of the settings at their default, and then select Review + create.

  5. Select the Create button once Azure validates your settings. Provisioning the database may take a few minutes.

A screenshot showing the form to fill out to create a new SQL database in Azure.

4 - Connect the App to the Database

Next, we must connect the App hosted in our App Service to our database using a Connection String. You can use Service Connector to create the connection.

Sign in to the Azure portal and follow the steps to create your Azure App Service resources:

Instructions Screenshot
In the Azure portal:
  1. Type the name of your app in the search box at the top of the screen.
  2. In the search results, select the app to navigate to it.
A screenshot showing how to locate the app service in the Azure portal.
  1. On the left navigation, select Service Connector.
  2. Select Create.
A screenshot showing how to locate Service Connector from the Azure portal.
On the Create connection page
  1. select or enter the following settings:

    • Service Type: Select SQL Database.
    • SQL server: Enter your SQL Database server name.
    • SQL database: Select coreDB.
  2. Select Next: Authentication.

A screenshot showing how to create a connection to the SQL database for the app in the Azure portal.
Under the Authentication tab:
  1. Specify the username and password of your SQL database.
  2. Select Next: Networking, then select Next: Review + Create.
A screenshot showing how to enter username and password of SQL Database during service connection in the Azure portal.
After validation is complete, select Create to create the service connection.It might take 1 minute to complete the operation. Click Refresh button to see the SQL database connection. A screenshot showing how to review and create the connection in the Azure portal.
In the Service Connector page:
  1. Expand the connection by selecting > next to it. AZURE_SQL_CONNECTIONSTRING is the connection string generated for you.
  2. Select Hidden value. Click to show value and copy the connection string for later.
Your app can now connect to the SQL database. Next, let's generate the schema for our data using Entity Framework Core.
A screenshot showing how to get the connection string for a service connector in the Azure portal.

5 - Generate the Database Schema

To generate our database schema, set up a firewall rule on the SQL database server. This rule lets your local computer connect to Azure. For this step, you'll need to know your local computer's IP address. For more information about how to find the IP address, see here.

In the Azure portal:

Instructions Screenshot
In the Azure portal:
  1. In the top search bar, search for the coredbserverXYZ server you created earlier and select it from the results.
A screenshot showing how to use the search box in the top tool bar to find database server for the app in the Azure portal.
  1. On the left navigation, select Firewalls and virtual networks.
  2. In the Firewall Rules section, enter a new Rule name of 'MyLocalAccess'. In the Start IP and End IP fields, paste the IP Address you copied earlier.
  3. Select Save at the top of the screen to persist your changes.
A screenshot showing how to allow access to the database server from your workstation's IP address in the Azure portal.

Next, update the appsettings.json file in the sample project with the connection string Azure SQL Database. The update allows us to run migrations locally against our database hosted in Azure. Replace the username and password placeholders with the values you chose when creating your database.

"AZURE_SQL_CONNECTIONSTRING": "Data Source=<your-server-name>.database.windows.net,1433;Initial Catalog=coreDb;User ID=<username>;Password=<password>"

Next, update the Startup.cs file the sample project by updating the existing connection string name MyDbConnection to AZURE_SQL_CONNECTIONSTRING:

services.AddDbContext<MyDatabaseContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING")));

From a local terminal, run the following commands to install the necessary CLI tools for Entity Framework Core, create an initial database migration file, and apply those changes to update the database:

cd <sample-root>\DotNetCoreSqlDb
dotnet tool install -g dotnet-ef
dotnet ef migrations add InitialCreate
dotnet ef database update

After the migration finishes, the correct schema is created.

If you receive the error Client with IP address xxx.xxx.xxx.xxx is not allowed to access the server, that means the IP address you entered into your Azure firewall rule is incorrect. To fix this issue, update the Azure firewall rule with the IP address provided in the error message.

6 - Deploy to the App Service

That we're able to create the schema in the database means that our .NET app can connect to the Azure database successfully with the new connection string. Remember that the service connector already configured the AZURE_SQL_CONNECTIONSTRING connection string in our App Service app. We're now ready to deploy our .NET app to the App Service.

Instructions Screenshot
In the Visual Studio solution explorer, right-click on the DotNetCoreSqlDb project node and select publish. A publishing workflow dialog will open. Select Azure as the deployment target and select next. A screenshot showing the publish dialog in Visual Studio.
Select Azure App Service (Windows) as the host of your app and then select Next. A screenshot showing how to select the deployment target in Azure.
If you're not already logged into an Azure account, you'll be prompted to do so. Select Sign In to launch the Azure sign-in page and then enter your account credentials. You can also select Create an Account to set up a new account if you don't have one already. A screenshot showing the sign-in to Azure dialog in Visual Studio.
On the App Service step, make sure your Subscription is selected, and then locate the App Service you created under the App Service instances selection box. Select that App Service and then select finish. A screenshot showing the dialog to select the App Service instance to deploy to in Visual Studio.
On the publishing profile summary view, select Publish to deploy your app to Azure. This process may take a few moments, but once it completes your app will be successfully published to Azure. A screenshot showing the publishing profile summary dialog in Visual Studio and the location of the publish button used to publish the app.

7 - Browse the Deployed Application and File Directory

Go back to your web app in the browser. You can always get back to your site by selecting the Browse link at the top of the App Service overview page. If you refresh the page, you can now create todos and see them displayed on the home page. Congratulations!

A screenshot showing the app successfully deployed to Azure.

Next, let's take a closer look at the deployed files of our app using a tool called Kudu.

Azure App Service provides a web-based diagnostics console named Kudu. Kudu lets you examine the server-hosting environment, view deployed files to Azure, review deployment history, and even open an SSH session into the hosting environment.

To use Kudu, go to one of the following URLs. You'll need to sign into the Kudu site with your Azure credentials.

  • For apps deployed in Free, Shared, Basic, Standard, and Premium App Service plans - https://<app-name>.scm.azurewebsites.net
  • For apps deployed in Isolated service plans - https://<app-name>.scm.<ase-name>.p.azurewebsites.net From the main page in Kudu, you can find information about the application-hosting environment, app settings, deployments, and browse the files in the wwwroot directory.

A screenshot showing the Kudu admin page.

8 - Configure and Stream Application Logs

Azure App Service captures messages logged to the console to assist you in diagnosing issues with your application. The sample app outputs console log messages in each of its endpoints to demonstrate this capability. The contents of the App Service diagnostic logs can be reviewed in the Azure portal, Visual Studio Code, or using the Azure CLI.

Instructions Screenshot
First, you need to enabled streaming logs in Azure App Service. Navigate to the page for the App Service instance in the Azure portal.

  1. Select the App Service logs under the Monitoring heading in the menu on the left side of the page.
  2. Change the Application Logging (File System) property to On.
  3. Enter a retention period of 30 days for the logs.
  4. Select Save to save your changes.
A screenshot showing how to enable logging for the web app in the Azure portal.
Select the Log stream item from the menu under the Monitoring section. Refresh the home page in the app or attempt other requests to generate some log messages.

You will see any log messages generated by your app and messages generated by the service in the output.
A screenshot showing application logs in the Azure portal.

Clean up resources

When you're finished, you can delete all of the resources from Azure by deleting the resource group for the application. It deletes all of the resources contained inside the group.

Follow these steps while signed-in to the Azure portal to delete a resource group:

Instructions Screenshot
  1. In the Azure portal, enter the name of the resource group in the search bar.
  2. Select the name of the resource group in the search results to navigate to it.
A screenshot showing how to search for and navigate to a resource group in the Azure portal.
Select the Delete resource group button at the top of the page. A screenshot showing the location of the Delete Resource Group button in the Azure portal.
In the confirmation dialog, enter the name of the resource group to confirm deletion. Select the Delete button at the bottom of the page to delete the resource group. A screenshot of the confirmation dialog for deleting a resource group in the Azure portal.

Next steps

Advance to the next tutorial to learn how to map a custom DNS name to your app.

Or, check out other resources: