Entity Framework  

Entity Framework Core (EF Core) Health Checks — A Complete Guide

Modern .NET applications rely heavily on databases, and issues such as connection failures, timeouts, or degraded performance can bring an entire system down. Health checks in ASP.NET Core provide a standardized way to monitor the health of your application and its dependencies—especially critical ones like Entity Framework Core (EF Core).

This article explains how EF Core health checks work, why they are important, and how to implement them in your application.

What Are Health Checks in ASP.NET Core?

Health checks are diagnostic endpoints that verify whether an application and its essential components are functional. The ASP.NET Core Health Checks system allows you to:

  • Monitor database connectivity

  • Track availability of external services

  • Integrate with observers such as Kubernetes, Docker, or monitoring tools

  • Build custom checks for business-specific logic

For EF Core, the goal is simple: ensure your database is reachable and operational.

Why EF Core Health Checks Are Important

An application may start correctly but still fail due to issues with its database. EF Core health checks help detect problems such as:

  • Incorrect database connection strings

  • Network connectivity issues

  • Database server downtime

  • Authentication/authorization failures

  • Long-running queries and performance degradation

These checks help prevent unexpected downtime and allow orchestration platforms (like Kubernetes) to automatically restart or replace bad instances.

How EF Core Health Checks Work

ASP.NET Core provides health checks through the Microsoft.Extensions.Diagnostics.HealthChecks package. You can add a health check for EF Core by using:

services.AddHealthChecks()
        .AddDbContextCheck<ApplicationDbContext>();

This runs a lightweight EF Core operation (usually a simple database query). If it succeeds, the database is considered healthy.

Implementing EF Core Health Checks (Step by Step)

1. Install Required Packages

If not already installed:

dotnet add package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore

2. Configure Health Checks in Program.cs

using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// EF Core DbContext
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
);

// Register Health Checks
builder.Services.AddHealthChecks()
    .AddDbContextCheck<ApplicationDbContext>("Database");

var app = builder.Build();

// Map health check endpoint
app.MapHealthChecks("/health");

app.Run();

What this does

  • Creates a /health endpoint

  • Performs a check using EF Core

  • Returns Healthy, Degraded, or Unhealthy

3. Custom Health Check Response (Optional)

You can format your health check result using:

app.MapHealthChecks("/health", new HealthCheckOptions
{
    ResponseWriter = async (context, report) =>
    {
        context.Response.ContentType = "application/json";
        var json = new
        {
            status = report.Status.ToString(),
            results = report.Entries.Select(x => new {
                name = x.Key,
                status = x.Value.Status.ToString(),
                description = x.Value.Description
            })
        };
        await context.Response.WriteAsJsonAsync(json);
    }
});

This is helpful when integrating with tools like Prometheus, Grafana, or Kibana.

Healthy vs Unhealthy Database Check Example

Healthy Output

{
  "status": "Healthy",
  "results": [
    {
      "name": "Database",
      "status": "Healthy"
    }
  ]
}

Unhealthy Output

{
  "status": "Unhealthy",
  "results": [
    {
      "name": "Database",
      "status": "Unhealthy",
      "description": "Unable to connect to SQL Server"
    }
  ]
}

Best Practices

  • Keep health check queries lightweight.

  • Use tags for organized monitoring.

  • Expose separate endpoints for readiness and liveness.

  • Avoid expensive queries or migrations in health checks.

  • Monitor performance; health checks run frequently in production.

Conclusion

Entity Framework Core Health Checks provide a powerful and straightforward way to ensure your application's database connectivity and stability. By integrating these checks into your application, you improve reliability, reduce downtime, and enable smarter orchestration in cloud-native environments.

EF Core health checks are easy to implement, highly customizable, and essential for modern production-ready systems.