- Published on
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
dotnet 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.
dotnet add package Volo.Abp.Autofac
dotnet add package Volo.Abp.AspNetCore.Mvc
dotnet add package Volo.Abp.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
Create Entity
We will create a simple Book Entity.
public class Book : AuditedAggregateRoot<Guid>
{
public Book(Guid id, string name)
{
this.Id = id;
this.Name = name;
}
public string Name { get; set; }
}
Create DB Context
public class MyDbContext : AbpDbContext<MyDbContext>
{
public DbSet<Book> Books => Set<Book>();
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite();
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Book>(b =>
{
b.ToTable("Books");
b.ConfigureByConvention();
b.HasData(new Book(Guid.NewGuid(),"My Book"));
});
}
}
This db context will configure the entity and also seed the database with one data.
Create the minimal module
[DependsOn(
typeof(AbpAspNetCoreMvcModule),
typeof(AbpAutofacModule),
typeof(AbpEntityFrameworkCoreSqliteModule)
)]
public class MinimalModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext<MyDbContext>(options =>
{
options.AddDefaultRepositories(includeAllEntities: true);
});
Configure<AbpDbContextOptions>(options =>
{
options.UseSqlite();
});
}
}
Create the minimal api
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddAppSettingsSecretsJson()
.UseAutofac();
builder.Services.ReplaceConfiguration(builder.Configuration);
builder.Services.AddApplication<MinimalModule>();
var app = builder.Build();
app.MapGet("/book", async ([FromServices] IRepository<Book, Guid> repository) =>
{
return await repository.GetListAsync();
});
app.InitializeApplication();
app.Run();
We have one GET request which will return the data from the db.
Add connection string
update the appsettings.json with the ConnectionStrings
"ConnectionStrings": {
"Default": "Filename=./db.sqlite"
}
Create migration
Now our app is ready lets create migrations for the DBcontext.
dotnet ef migrations add init
Apply migrations to DB
dotnet ef database update
Run the App
dotnet 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
Related Posts
Continue reading with these related articles
Building AI-Powered Sentiment Analysis with Microsoft Agent Framework and Ollama
Learn how to build sentiment analysis with Microsoft Agent Framework and Ollama. We will use the Ollama model to perform the sentiment analysis and the Microsoft Agent Framework to build the agent.
Building AI Agents with Microsoft Agent Framework and Ollama: A Getting Started Guide
Learn how to build sophisticated AI agents using Microsoft Agent Framework with Ollama for local AI model execution. This comprehensive guide covers streaming responses, multi-turn conversations, function tools, middleware integration, and production-ready patterns.
ABP React CMS Module: Building Dynamic Pages with Puck Editor
ABP React CMS Kit is a React UI for the ABP CmsKit module.