Building Minimal API endpoints from EF Core DbContext

July 08, 2022 by Anuraj

AspNetCore EFCore

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; }
    [Required]
    public string? Name { get; set; }
    [Required]
    [EmailAddress]
    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.

builder.Services.AddDbContext<PersonDbContext>
    (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);

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

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

var app = builder.Build();

app.MapInstantAPIs<PersonDbContext>();

app.Run();

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 :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub