NPv.DataAccess.Ef
1.3.1
dotnet add package NPv.DataAccess.Ef --version 1.3.1
NuGet\Install-Package NPv.DataAccess.Ef -Version 1.3.1
<PackageReference Include="NPv.DataAccess.Ef" Version="1.3.1" />
<PackageVersion Include="NPv.DataAccess.Ef" Version="1.3.1" />
<PackageReference Include="NPv.DataAccess.Ef" />
paket add NPv.DataAccess.Ef --version 1.3.1
#r "nuget: NPv.DataAccess.Ef, 1.3.1"
#:package NPv.DataAccess.Ef@1.3.1
#addin nuget:?package=NPv.DataAccess.Ef&version=1.3.1
#tool nuget:?package=NPv.DataAccess.Ef&version=1.3.1
NPv.DataAccess.Ef
Pluggable EF Core infrastructure for a generic repository and a single explicit unit of work.
Read-side queries are expected to go through Dapper, completely bypassing this library.
π¦ Version 1.3.x β Explicit Commit & Safe UoW
Overview
This library provides:
- A reusable
EfCoreGenericRepository
for standard CRUD operations (write path only) - A scoped
IDbContextProvider
with explicit commit semantics- No auto-commit on dispose
- No transaction management exposed to callers
- One unit of work = one
SaveChangesAsync()
wrapped in a transaction
- Enhanced mapping conventions for entities and string fields
- Value generation using
GuidV7ValueGenerator
for sequential GUIDs [LongText]
attribute and defaultnvarchar(256)
string mappings
Designed as a drop-in infrastructure layer for services, APIs, and background workers that rely on EF Core.
π Getting Started
- Add a reference to the package.
- Register your
DbContext
,IDbContextProvider
, and repository in your DI container. - Use the repository inside command handlers.
- Call
CommitAsync()
once per unit of work.
β Example Setup
Define your application DbContext
:
public class ApplicationContext(DbContextOptions<ApplicationContext> options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
foreach (var entityType in builder.Model.GetEntityTypes()
.Where(t => t.ClrType.IsSubclassOf(typeof(Entity))))
{
var entity = builder.Entity(entityType.ClrType);
entity.Property("Id")
.ValueGeneratedOnAdd()
.HasValueGenerator<GuidV7ValueGenerator>();
}
// Applies default mapping for strings to nvarchar(256)
DefaultString256MaxLengthMapping.ApplyMapping(builder);
}
}
π Usage Notes
- All string columns default to
nvarchar(256)
unless[MaxLength]
or[LongText]
is applied. - Entity
Id
uses a sequentialGuidV7
for better index locality. DbContextProvider
only commits viaCommitAsync()
; never on dispose.- For commands (writes), wrap execution in a unit of work and call
CommitAsync()
only once at the end. - For queries (reads), use Dapper directly β EF Core is bypassed.
Example Command Handler
public sealed class CreateOrderHandler(IGenericRepository repo)
{
public async Task ExecuteAsync(CreateOrder cmd)
{
var order = new Order { /* ... */ };
await repo.AddAsync(order);
// No SaveChanges here!
}
}
// Somewhere in your pipeline:
await using var uow = scope.Resolve<IDbContextProvider>();
await handler.ExecuteAsync(command);
await uow.CommitAsync(); // wraps SaveChanges in a transaction
Author's Note
This library grew out of my long-standing personal interest in structuring and publishing open source packages. Over time, Iβve revisited and refined earlier internal utilities and ideas, giving them a more consistent shape and preparing them for wider reuse. Along the way, Iβve also taken the opportunity to explore how open source distribution and licensing work in the .NET ecosystem.
Itβs a small step toward something Iβve always wanted to try β sharing practical, minimal tools that reflect years of learning, experimentation, and refinement.
Hopefully, someone finds it useful.
Nikolai π
βοΈ License
MIT β you are free to use this in commercial and open-source software.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Autofac (>= 8.4.0)
- Microsoft.EntityFrameworkCore (>= 9.0.8)
- NPv.DataAccess.Abstractions (>= 1.1.1)
- NPv.Domain.Core (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.