Anto Subash.

Minimal Api with ABP - EF Core - Part 2

Table of contents

Intro

In this we will continue with the last one and add Ef core to our Minimal ABP module.

This is a continuation of Part 1

Create the project

1dotnet new web -n MinimalEFWithAbp

Navigate to the MinimalEFWithAbp folder and add the required packages.

Add required packages

To add the required packages use the dotnet add package command.

1dotnet add package Volo.Abp.Autofac
2dotnet add package Volo.Abp.AspNetCore.Mvc
3dotnet add package Volo.Abp.EntityFrameworkCore.Sqlite
4dotnet add package Microsoft.EntityFrameworkCore.Design

Create Entity

We will create a simple Book Entity.

1public class Book : AuditedAggregateRoot<Guid>
2{
3    public Book(Guid id, string name)
4    {
5        this.Id = id;
6        this.Name = name;
7    }
8    public string Name { get; set; }
9}

Create DB Context

1public class MyDbContext : AbpDbContext<MyDbContext>
2{
3    public DbSet<Book> Books => Set<Book>();
4
5    public MyDbContext(DbContextOptions<MyDbContext> options)
6        : base(options)
7    {
8    }
9
10    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
11    {
12        base.OnConfiguring(optionsBuilder);
13        optionsBuilder.UseSqlite();
14    }
15
16    protected override void OnModelCreating(ModelBuilder builder)
17    {
18        base.OnModelCreating(builder);
19        builder.Entity<Book>(b =>
20        {
21            b.ToTable("Books");
22            b.ConfigureByConvention();
23            b.HasData(new Book(Guid.NewGuid(),"My Book"));
24        });
25    }
26}

This db context will configure the entity and also seed the database with one data.

Create the minimal module

1[DependsOn(
2    typeof(AbpAspNetCoreMvcModule),
3    typeof(AbpAutofacModule),
4    typeof(AbpEntityFrameworkCoreSqliteModule)
5)]
6public class MinimalModule : AbpModule
7{
8    public override void ConfigureServices(ServiceConfigurationContext context)
9    {     
10        context.Services.AddAbpDbContext<MyDbContext>(options =>
11        {
12            options.AddDefaultRepositories(includeAllEntities: true);
13        }); 
14        Configure<AbpDbContextOptions>(options =>
15        {
16            options.UseSqlite();
17        });
18    }
19}

Create the minimal api

1var builder = WebApplication.CreateBuilder(args);
2builder.Host.AddAppSettingsSecretsJson()
3    .UseAutofac();
4builder.Services.ReplaceConfiguration(builder.Configuration);
5builder.Services.AddApplication<MinimalModule>();
6var app = builder.Build();
7
8app.MapGet("/book", async ([FromServices] IRepository<Book, Guid> repository) =>
9{
10    return await repository.GetListAsync();
11});
12
13app.InitializeApplication();
14app.Run();

We have one GET request which will return the data from the db.

Add connection string

update the appsettings.json with the ConnectionStrings

1"ConnectionStrings": {
2    "Default": "Filename=./db.sqlite"
3}

Create migration

Now our app is ready lets create migrations for the DBcontext.

1dotnet ef migrations add init

Apply migrations to DB

1dotnet ef database update

Run the App

1dotnet run

View the Book

Once the application is launched navigate to /book you will see the seeded book as a json response.

Repo : https://github.com/antosubash/AbpMinimalApiWithEFCore

Buy Me a Coffee at ko-fi.com