Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare Azure environments for new workloads—subscriptions, networking, identity, and landing zones
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/app-service/templates/recipes/sql/source/dotnet.md
1# SQL Database — C# (.NET) — REFERENCE ONLY23## Entity Framework Core Setup45Add EF Core with Azure SQL and managed identity support to an ASP.NET Core app.67### NuGet Packages89```xml10<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.*" />11<PackageReference Include="Azure.Identity" Version="1.13.*" />12<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.*" />13```1415### DbContext1617Create `Data/AppDbContext.cs`:1819```csharp20using Microsoft.EntityFrameworkCore;2122namespace MyApp.Data;2324public class AppDbContext : DbContext25{26public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }2728public DbSet<TodoItem> TodoItems => Set<TodoItem>();29}3031public class TodoItem32{33public int Id { get; set; }34public string Title { get; set; } = string.Empty;35public bool IsComplete { get; set; }36}37```3839### Program.cs (additions)4041Add these lines to `Program.cs` — do NOT replace the file:4243```csharp44using Microsoft.EntityFrameworkCore;45using MyApp.Data;4647// Add after builder creation48var connectionString = builder.Configuration.GetConnectionString("AZURE_SQL")49?? builder.Configuration["AZURE_SQL_CONNECTION_STRING"];5051builder.Services.AddDbContext<AppDbContext>(options =>52options.UseSqlServer(connectionString));5354// Add health check for SQL55builder.Services.AddHealthChecks()56.AddDbContextCheck<AppDbContext>();5758// After app build — map health checks so App Service probes reflect SQL connectivity59app.MapHealthChecks("/health");60```6162### API Endpoints6364Add to `Program.cs` after `app` is built:6566```csharp67app.MapGet("/api/todos", async (AppDbContext db) =>68await db.TodoItems.ToListAsync());6970app.MapGet("/api/todos/{id}", async (int id, AppDbContext db) =>71await db.TodoItems.FindAsync(id) is TodoItem todo72? Results.Ok(todo)73: Results.NotFound());7475app.MapPost("/api/todos", async (TodoItem todo, AppDbContext db) =>76{77db.TodoItems.Add(todo);78await db.SaveChangesAsync();79return Results.Created($"/api/todos/{todo.Id}", todo);80});81```8283### appsettings.json8485```json86{87"ConnectionStrings": {88"AZURE_SQL": "Server=localhost;Database=myapp;Trusted_Connection=true;"89}90}91```9293> In production, the `AZURE_SQL_CONNECTION_STRING` app setting from Azure overrides this with the managed identity connection string.9495### EF Migrations (postprovision hook)9697Create `infra/scripts/setup-db.sh`:9899```bash100#!/bin/bash101dotnet ef database update --project src/api102```103104## Files to Add105106| File | Action |107|------|--------|108| `Data/AppDbContext.cs` | Create — DbContext + entity models |109| `Program.cs` | Modify — add DbContext registration + endpoints |110| `appsettings.json` | Modify — add ConnectionStrings section |111112## Common Patterns113114- Always use `AddHealthChecks().AddDbContextCheck<>()` for SQL health monitoring115- Use `AsNoTracking()` for read-only queries to improve performance116- Apply migrations via postprovision hook, not at app startup117