ASP.NET Core API connect to Azure SQL Database

Entity Framework (EF) Package — Object Relational Mapper (ORM)

An object-relational mapper (O/RM) that enables .NET developers to persist objects to and from a data source — eliminates the need for most of the data access code developers would typically need to write.

  1. Add EF package using CLI or NuGet Package Manager
  2. Check the newly installed package under NuGet directory

Connection Strings — appsettings.json

Will be used ConfigureServices — GetConnectionString() method in Startup.cs

“ConnectionStrings”: {“mydatabaseapp”: “Server=tcp:<databasehost>,1433;Initial Catalog=<databasename>;Persist Security Info=False;User ID=<username>;Password=<passowrd>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;”}

DbContext — Hold properties representing collections for entities

Must have a constructor that accepts DbContextOptions and passes this argument to the base DbContext constructor.

Create a new subclass of DBContext

public class ClientDbContext : DbContext{   public ClientDbContext(DbContextOptions<ClientDbContext> options): base(options){}   public DbSet<Client> Clients { get; set; }}


Startup.cs — Configure the app’s services

To configure DbContext subclass to use a SQL Server database with a connection string defined in Configuration — use .AddDbContext

public void ConfigureServices(IServiceCollection services){   services.AddControllers();   services.AddDbContext<ClientDbContext>(opts => opts.UseSqlServer(Configuration.GetConnectionString(“mydatabaseapp”)));}


Responding to request

[Route(“api”)][ApiController]public class ClientController : ControllerBase{   private readonly ILogger<ClientController> _logger;   private readonly ClientDbContext _context;   public ClientController(ILogger<ClientController> logger, ClientDbContext context){   _logger = logger;   _context = context;}[HttpGet][Route(“clients”)]public IActionResult GetAll(){   _logger.LogInformation(“Get all clients”);   var data = _context.Clients.AsQueryable();   return Ok(data);}}

Entity / Model

Entity properties to map table columns

[Table(“Client”)]public class Client{[Column(“client_id”)]public int ClientId { get; set; }
[Column(“client_name”)]public string ClientName { get; set; }
[Column(“client_address”)]public string ClientAddress { get; set; }}




Software engineering and film. All are work-in-progress.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Development updates

5 Steps to solving technical debt

A young man enters data on a scrum card.

Check for presence of 2 sub-strings/characters in an excel cell

Getting started with Android Development [Java] — Make your first Android App

Git: Connecting a Project to a Repository

FunctionLand — Own your personal cloud storage hardware and be rewarded for it

A Network That Networks — Part 1: Scenario Based Profile


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Software engineering and film. All are work-in-progress.

More from Medium

How To Create A Virtual Machine Using Azure Portal and How To Connect Through RDP (Remote Desktop…

How to add a free SSL certificate to Azure App Service (Auto Renewable)

Contract Deployment Automation for Azure API Management integration with PowerApps

VGG Net Architecture