Create and Configure a SQL Server Database and ASP.NET Core Web API in Azure: Dev To Azure - Part 2

22nd October 2020

We will look at how to set up a SQL Server database and ASP.NET Core Web API in Azure. In addition, we will allow the Web API to communicate with the database.

In part 1, we set up an ASP.NET Core Web API and Blazor Wasm application into Azure Devops. By using the Repos section in a Azure project, we were able to push local Git repositories into Azure Devops.

For this tutorial, we are going to build the Web API repository from Azure Devops. Then, we will set up a pipeline where we can release it to an Azure server.

How To Deploy a Database Into Azure

Before we set up the Web API, we need to deploy the database into Azure.

First, we need to go into Azure Portal and go to the SQL Servers page.

Create a SQL Server in Azure
Create a SQL Server in Azure. You can search for it if it doesn't appear at the top.

Go ahead and create a new SQL Server.

We will need to fill out some details, such as the server name, login and password.

In addition, we need to make sure we go to the Networking tab and ensure that "Allow Azure services and resources to access this server" is turned on.

Allow Azure Services to access your SQL Server database
Make sure that you turn on "Allow Azure Services to access your SQL Server database"

Once that's done, go ahead and create the server. It can take a bit of time to deploy a server to Azure, so patience may be the key.

Next, we need to open up SQL Server Management Studio (SSMS), and located the database we wish to deploy to Azure.

From there, we can right-click, go to Tasks and go to Deploy Database to Microsoft Azure SQL Database

Deploy a SQL Server Database to Azure in SSMS
Deploy a SQL Server Database to Azure in SSMS

We will need to log in to our Azure account and whitelist our IP address to the Azure SQL Server.

Once we have been through the deployment steps, SSMS will go ahead and deploy the database to Azure.

How To Setup an ASP.NET Core Web API in Azure?

Next, we need to setup the ASP.NET Core Web API server in Azure.

But first, we need to make a note of the connection string for our SQL Server database in Azure.

Locate the SQL Server in Azure. Then, go to Settings and SQL databases.

Located to SQL databases in SQL Server in Azure
Located to SQL databases in SQL Server in Azure

Click on the database, and go to the link that says "Show database connection strings".

Show Database Connection Strings for SQL Server Database in Azure
Show Database Connection Strings for SQL Server Database in Azure

It will then display the connection string (without the password). Make a note, by copying and pasting the connection string into something like Notepad.

Now it's time to create the server for the API app.

Search for "API App" in Azure to create a new one.

Create an API App in Azure
Create an API App in Azure by searching for it

Fill out the form and press the Create button.

Once the API has been created, we need to go in to it. Then navigate to Settings and Configuration.

Inside the "Application settings" tab, scroll down to the Connection string section.

For the name, add the same name that is contained in the ConnectionStrings section in the appsettings.json file.

For the value, copy and paste the connection string from the SQL Server database that we just set up in Azure, making sure to include the password in the connection string.

We also need to select a Type. Make sure it's set to SQLServer.

Add a Connection String to an API App in Azure
Add a Connection String to an API App in Azure

Afterwards, we need to go into the "General Settings" tab and make sure that the stack is set to ".NET Core".

Select the Stack as .NET Core in Api App
Select the Stack as .NET Core in Api App

In addition, we selected the Platform as 64 Bit and turned on Web Sockets. Web Sockets needs to be turned on if SignalR is being used.

Once we have made the changes, go ahead and click on the Save button.

What About Deploying The Web API To Azure?

The next job is to get the application from our ASP.NET Core Web API repository in Azure Devops, and deploy it to Azure.

By setting up a deployment, if a commit is made to that particular branch in that repository, it will automatic do a deployment to Azure.

This is known as continuous integration and deployment.

Inside the API app, we can go to Deployment and Deployment Center.

Setup Continuous Integration in Azure
Setup Continuous Integration in Azure

We have a number of steps which we need to go through.

The first is to select which source control we are using. We are using Azure Devops, but others can be selected like GitHub and Bitbucket.

Select Azure Pipelines and we need to select our Azure organisation. This will bring up the relevant Azure Devops project, repository and branch.

Once we have pressed the Released button, it will create a new pipeline in our Azure Devops Repo.

It will checkout and build our repository from Azure Devops. Thereafter, it will do a release and deploy the files to our API app in Azure.

This takes a little bit of time, but once done, our API should be working in Azure.

When Will Our App Be Deployed?

Watch our tutorial where we show the area that will notify us when the app has been deployed to Azure.

In addition, we will demonstrate the steps to set up a SQL Server in Azure and deploy a database from SSMS to Azure.

Afterwards, we set up an API app in Azure and also demonstrate how to set the connection string.

Finally, we walk through the steps on how to set up continuous integration in Azure. This is where we deploy an Azure Devops repository to an Azure server.

What About The Blazor Wasm Application?

In the next part, we will walk through the steps in deploying the Blazor Wasm application to Azure.

It's similar to what we've done with the ASP.NET Core Web API. However, we also have to give permission to the Blazor Wasm application to the Web API. We can do that by using CORS and this can be done in Azure.

That's all coming up in part 3.

About the author

David Grace

David Grace

Senior .NET web developer | ASP.NET Core | C# | Software developer

Free .NET videos

  • Do you want to watch free videos featuring .NET 7 new features?
  • How about what's new in C# 11?
  • Or a recap on the SOLID principles?
Watch our .NET videos