ASP.NET Core API connect to Azure SQL Database

SQL Database Integration

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

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

Connection Strings — appsettings.json

“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

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


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


[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

[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.

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