DEV Community

Cover image for EF Core and Azure SQL Database: A Step-by-Step Guide
Anton Martyniuk
Anton Martyniuk

Posted on Originally published at antondevtips.com

EF Core and Azure SQL Database: A Step-by-Step Guide

EF Core is the most popular way to work with a database in .NET.

Azure SQL Database is the managed SQL Server you get in the cloud, with backups, patching, and high availability handled for you.

Putting the two together is straightforward, but the cloud adds a few things you never deal with on a local SQL Server.

Connections drop under load. The firewall blocks you by default. And a database password sitting in your connection string is a risk you want to remove before production.

Today I want to present you with a complete guide to getting started with Azure SQL Database in .NET.

In this post, we will explore:

  • What Azure SQL Database is
  • How to create an Azure SQL Database in the Azure Portal
  • Connecting EF Core to Azure SQL
  • Running EF Core migrations against Azure SQL
  • Adding connection resilience for transient faults
  • Setting up Azure SQL with .NET Aspire
  • Passwordless authentication with Microsoft Entra ID

Let's dive in.


👉 Read original article on my newsletter: https://antondevtips.com/blog/ef-core-and-azure-sql-database

What Is Azure SQL Database

Azure SQL Database is a fully managed database service built on the SQL Server engine.

Because it is the same engine, EF Core talks to it through the exact provider you already use for SQL Server.
You do not need a special "Azure" provider - only a connection string that points to the cloud.

Azure manages the parts you do not want to: the operating system, patching, backups, and failover.
You manage your schema and your data.

You pick from two main purchasing models:

  • Provisioned - a fixed amount of compute that is always on. Predictable cost for steady workloads.
  • Serverless - compute that scales automatically and pauses when idle. You pay per second of use, which is ideal for development and test environments.

First, let's create one.

Creating an Azure SQL Database in the Azure Portal

Follow these steps:

1. Open the Azure Portal, search for "SQL databases", and click Create.

Screenshot_1

2. On the next page, select your Azure subscription and click Create:

Screenshot_2

3. Enter your database name and set up a logical SQL server to host the database:

Screenshot_3

4. Create a new one, select a location, give it a unique name, set the identification method, and set an admin login and password.

Screenshot_4

5. For the compute tier, choose Serverless while you are testing.
It pauses when idle, so you only pay while you use it.

Screenshot_5

6. After the database is created, you need to open the firewall. By default, Azure SQL blocks every connection.

Go to the server's Networking settings, and add your client IP address so you can connect from your machine.
Turn on "Allow Azure services and resources to access this server" so apps you deploy in Azure can connect to it.

Screenshot_6

Note: The firewall is the most common reason a first connection fails. If you see a "Cannot open server" error, check that your current IP is in the firewall rules.

With the database ready, let's connect EF Core to it.


👉 Read original article on my newsletter: https://antondevtips.com/blog/ef-core-and-azure-sql-database

Top comments (0)