ZibStack.NET.EntityFramework
EF Core integration for ZibStack.NET.Dto CRUD API. Auto-generates EfCrudStore implementations and DI registration from your DbContext.
See the working sample: SampleApi on GitHub
Install
Section titled “Install”dotnet add package ZibStack.NET.Dtodotnet add package ZibStack.NET.EntityFrameworkdotnet add package Microsoft.EntityFrameworkCore.Sqlite # or any EF Core providerQuick start
Section titled “Quick start”- Define your entity with
[CrudApi]:
[CrudApi][CreateDto][UpdateDto][ResponseDto]public class Player{ [DtoIgnore(DtoTarget.Create | DtoTarget.Update | DtoTarget.Query)] public int Id { get; set; } public required string Name { get; set; } public int Level { get; set; }}- Add
[GenerateCrudStores]to yourDbContext:
using ZibStack.NET.EntityFramework;
[GenerateCrudStores]public class AppDbContext : DbContext{ public DbSet<Player> Players => Set<Player>();
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }}- Wire up in
Program.cs:
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlite("Data Source=app.db"));
builder.Services.AddAppDbContextCrudStores(); // auto-generated!
var app = builder.Build();app.MapPlayerEndpoints(); // auto-generated by ZibStack.NET.Dtoapp.Run();That’s it. Full CRUD API backed by EF Core + SQLite.
What gets generated
Section titled “What gets generated”For each DbSet<T> property on your DbContext, the generator emits:
// Concrete storepublic class PlayerEfStore : EfCrudStore<Player, int, AppDbContext>{ public PlayerEfStore(AppDbContext db) : base(db) { } protected override DbSet<Player> Set => Db.Players;}
// DI registration extensionpublic static class AppDbContextCrudStoreExtensions{ public static IServiceCollection AddAppDbContextCrudStores( this IServiceCollection services) { services.AddScoped<ICrudStore<Player, int>, PlayerEfStore>(); return services; }}Key detection
Section titled “Key detection”The generator finds the primary key automatically:
- Property with
[Key]attribute - Property named
Id - Property named
{ClassName}Id
EfCrudStore<TEntity, TKey, TContext>
Section titled “EfCrudStore<TEntity, TKey, TContext>”Base class implementing ICrudStore<TEntity, TKey> using EF Core:
| Method | Implementation |
|---|---|
GetByIdAsync | DbSet.FindAsync |
Query | DbSet.AsQueryable() |
CreateAsync | DbSet.Add + SaveChangesAsync |
UpdateAsync | SaveChangesAsync (relies on EF change tracking) |
DeleteAsync | DbSet.Remove + SaveChangesAsync |
All methods are virtual — override any operation for custom behavior:
public class PlayerStore : EfCrudStore<Player, int, AppDbContext>{ public PlayerStore(AppDbContext db) : base(db) { } protected override DbSet<Player> Set => Db.Players;
public override async ValueTask CreateAsync(Player entity, CancellationToken ct = default) { entity.CreatedAt = DateTime.UtcNow; await base.CreateAsync(entity, ct); }}When using a custom store, register it manually instead of using AddAppDbContextCrudStores():
builder.Services.AddScoped<ICrudStore<Player, int>, PlayerStore>();How it works
Section titled “How it works”This package is a source generator. When your project references both ZibStack.NET.Dto (which provides ICrudStore) and Microsoft.EntityFrameworkCore, the generator emits the EfCrudStore base class and concrete implementations into your compilation. No runtime dependency on this package.