Building Minimal API endpoints from EF Core DbContext

This post is about building Minimal API endpoints from EF Core DbContext. When building APIs using EF Core and Minimal APIs, most of the time we will be writing the same code again and again. Recently I found a nuget package - InstantAPIs - this package helps to generate CRUD APIs with Swagger (Open API) definition from DbContext class with two lines of code.

To get started I am creating an empty web application using the command dotnet new web -o MinimalWebApiExample. Once it is done, I am adding reference of following nuget packages.

dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package InstantAPIs

Next I am creating a Model and DbContext class, like this.

public class Person
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Email { get; set; }
    public string? Address { get; set; }
    public DateTime CreatedOn { get; set; } = DateTime.UtcNow;

And here is the DbContext class.

public class PersonDbContext : DbContext
    public PersonDbContext(DbContextOptions options) 
        : base(options)

    protected PersonDbContext()
    public DbSet<Person>? Persons { get; set; } = null!;

Once it is done, add connection strings in appsettings.json file, modify the Program.cs to include DbContext class.

    (options => options.UseSqlServer(builder.Configuration.GetConnectionString("PersonDbContext")));

Next create and apply migrations using following commands.

dotnet ef migrations add InitialMigrations
dotnet ef database update

Next we can generate Minimal API endpoints using InstantAPIs. To do this, update the Program.cs file like this.

using InstantAPIs;

using Microsoft.EntityFrameworkCore;
using MinimalWebApiExample.Models;

var builder = WebApplication.CreateBuilder(args);

    (options => options.UseSqlServer(builder.Configuration.GetConnectionString("PersonDbContext")));

builder.Services.AddInstantAPIs(options => options.EnableSwagger = EnableSwagger.Always);

var app = builder.Build();



Now let us run the application using dotnet run command. And browse the application and navigate to /swagger endpoint, you will be able to see the API endpoints for Person object like this.

Scaffolding database with EF Core

Check out the Instant APIs project from GitHub

I found one issue when I tried to build minimal APIs with scaffolded entities and db context with NorthWind database. Later I identified the issue - InstantApis package generate endpoints using Primary key in the table / entity - for some entities there was not primary key - it was attributed with [Keyless] attribute. InstantApis was not considering this attribute and throwing an exception. So if you’re generating the API using scaffolded database entities, make sure all the entities got a primary key.

Happy Programming :)