Deploy an ASP.NET Core App with EF Core and SQL Server to Azure

Sena Kılıçarslan
.NET Core
Published in
8 min readJan 1, 2020

--

In this post, I will show how to deploy an ASP.NET Core web application with EF Core and SQL Server to App Service on Azure. Besides, I will demonstrate how to create an AzureSQL Database and use this database from the application.

The sections of the post will be as follows:

  • About the App
  • Sign up for Microsoft Azure
  • Azure SQL Database
  • Connecting to Azure SQL Database
  • Modifying the App
  • Test From Local
  • Publish the App to Azure
  • Clean up the Resources

About the App

The application that we will deploy manages a database of TV Shows and its main page looks like below:

The app is an ASP.NET Core MVC web application and it uses EF Core to perform CRUD operations on a SQL Server database.

You can download the source code from this Github repository.

Sign up for Microsoft Azure

If you don’t have an Azure account, go to Microsoft Azure site and click Start Free button. Sign up with your Microsoft account and get $200 (for free) to explore any Azure service for 30 days.

Azure SQL Database

In this section, we will create our SQL database on Azure.

Azure SQL Database is a general-purpose relational database, provided as a managed service. With it, you can create a highly available and high-performance data storage layer for the applications and solutions in Azure. It’s based on the latest stable version of the Microsoft SQL Server database engine.

SQL Database is a fully managed service that has built-in high availability, backups, and other common maintenance operations. Microsoft handles all patching and updating of the SQL and operating system code. You don’t have to manage the underlying infrastructure.

You can learn more about Azure SQL database here.

Now, we will create our SQL database on Azure.

Go to Azure Portal and select Create a Resource -> SQL Database as shown below:

Select your Subscription and click Create new in Resource Group field:

In the Database Details part, click Create New in the Server field:

After filling the necessary fields, click Ok. Please note your login and password as we will need this to connect to this database in the next section.

In the next screen, enter your Database name and click Configure Database to choose the plan that suits your needs:

Next, go the Networking tab and update the tab as shown in the following image:

Here we add the current client IP address because we will need this to connect to this database from our local machine.

We allow the Azure services to connect to this server as we will need this when we publish our web application to Azure. As mentioned in the highlighted section above, this gives access to all Azure services.

If you want to learn more about setting Azure SQL server/database firewall rules, please refer to this page.

Click Review + Create and then click Create. When the deployment is complete, click Go to resource button. Your resource will resemble the following:

If you click the All resources on the left pane, you can see the resources that you created:

Connecting to Azure SQL Database

In this section, we will connect to the database that we created on Azure from our local machine using Visual Studio.

In Visual Studio select View -> SQL Server Object Explorer and click Add SQL Server icon. Then fill the necessary fields with your database information as shown in the following image:

and click Connect.

Modifying the App

After establishing the connection, you will see this database in the SQL Server Object Explorer. Right-click on the server name and select Properties and get the connection string from there:

Go to appsettings.json file in the project and add this connection string as follows:

Please note that you should update your password here as it is masked with stars.

In our scenario, we will use our local database for development and Azure SQL database for production. So, we need to add the following part to Startup.cs to provide the related connection string for each environment. Update the ConfigureServices method as follows:

The Database.Migrate() call helps you when it's run in Azure, because it automatically creates the databases that your .NET Core app needs, based on its migration configuration.

Test From Local

Now, we will test the application on our local computer.

Before that, we need to update the ASPNETCORE_ENVIRONMENT variable to use the Azure SQL database

Right-click on the project in Visual Studio and select Properties. Then change the variable as follows:

and then Save.

Now we can run our application.

In this test configuration, our application server is on localhost and database server is on Azure.

Run the application on the Visual Studio and click TVShowsApp on the web page and then Create New on the next page.

After creating our first record, it is shown below on the main page:

We can check the record created using SQL Server Object Explorer as well:

Now that we checked that we can use the Azure SQL database we created, we can revert the ASPNETCORE_ENVIRONMENTto Development.

Publish the App to Azure

In this section, we will deploy our application to App Service on Azure.

Azure App Service enables you to build and host web apps, mobile back ends, and RESTful APIs in the programming language of your choice without managing infrastructure. It offers auto-scaling and high availability, supports both Windows and Linux, and enables automated deployments from GitHub, Azure DevOps, or any Git repo.

We can deploy our application to App Service via automated deployment or manual deployment.

Azure supports automated deployment directly from several sources. The following options are available:

  • Azure DevOps
  • GitHub
  • Bitbucket
  • OneDrive
  • Dropbox

Below are options that you can use to manually push your code to Azure:

  • Git
  • az webapp up (Command-line interface)
  • Zipdeploy
  • Visual Studio
  • FTP/S

We will use the manual deployment with Visual Studio option.

In Visual Studio, right-click on the project and select Publish… Then click Create Profile in the next screen:

In the next dialog, enter your App service Name and select the Resource Group that you created when creating the Azure SQL database on Azure portal. Then, click New in the Hosting plan field and choose Free for Size and fill the Name and Location fields.

Then click OK and Create respectively.

Now, our publishing profile is created as seen in the following image:

If we click the Publish button, our application will be deployed to Azure.

Before that, we need to add ASPNETCORE_ENVIRONMENT variable to our app service in Azure. We can check and update the App Service that we created on Azure portal.

Go to Azure Portal and click the App Services and then click the app service that you created:

In the app service, click Configuration and click New application setting. In the next dialog, enter ASPNETCORE_ENVIRONMENT for Name and Production for Value. Then, a new application setting will be formed as seen below:

Click Save.

Now, we can publish our application to Azure. Go to Visual Studio and click the Publish button that we saw in the publishing profile above.

After the deployment operation is completed, the web page is launched automatically and the application’s URL is as follows:

https://[Your-app-name].azurewebsites.net

I created two more records and the main page of the application running on Azure looks like below now:

We can see the newly added records from the database as well:

Clean up the Resources

When you’re working in your own subscription, it’s a good idea at the end of a project to identify whether you still need the resources you created. Resources left running can cost you money. You can delete resources individually or delete the resource group to delete the entire set of resources.

Now we will delete the resource group to stop billing for all the resources used within the group.

Go to Azure portal and click Resource groups and then select the resource group that you created. You will get a screen like below:

As you see in the above image, we have four resources in the group. We created the SQL server and the database from the Azure portal. App service and the app service plan were created automatically from the Visual Studio during the creation of the publishing profile.

You can click the Delete Resource group link to delete all of the resources.

--

--

Sena Kılıçarslan
.NET Core

A software developer who loves learning new things and sharing these..