Skip to content

ZibStack.NET.EntityFramework

NuGet Source

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

dotnet add package ZibStack.NET.Dto
dotnet add package ZibStack.NET.EntityFramework
dotnet add package Microsoft.EntityFrameworkCore.Sqlite # or any EF Core provider
  1. 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; }
}
  1. Add [GenerateCrudStores] to your DbContext:
using ZibStack.NET.EntityFramework;
[GenerateCrudStores]
public class AppDbContext : DbContext
{
public DbSet<Player> Players => Set<Player>();
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
  1. 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.Dto
app.Run();

That’s it. Full CRUD API backed by EF Core + SQLite.

For each DbSet<T> property on your DbContext, the generator emits:

// Concrete store
public class PlayerEfStore : EfCrudStore<Player, int, AppDbContext>
{
public PlayerEfStore(AppDbContext db) : base(db) { }
protected override DbSet<Player> Set => Db.Players;
}
// DI registration extension
public static class AppDbContextCrudStoreExtensions
{
public static IServiceCollection AddAppDbContextCrudStores(
this IServiceCollection services)
{
services.AddScoped<ICrudStore<Player, int>, PlayerEfStore>();
return services;
}
}

The generator finds the primary key automatically:

  1. Property with [Key] attribute
  2. Property named Id
  3. Property named {ClassName}Id

Base class implementing ICrudStore<TEntity, TKey> using EF Core:

MethodImplementation
GetByIdAsyncDbSet.FindAsync
QueryDbSet.AsQueryable()
CreateAsyncDbSet.Add + SaveChangesAsync
UpdateAsyncSaveChangesAsync (relies on EF change tracking)
DeleteAsyncDbSet.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>();

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.