DKNet.EfCore.Hooks
10.1.19
dotnet add package DKNet.EfCore.Hooks --version 10.1.19
NuGet\Install-Package DKNet.EfCore.Hooks -Version 10.1.19
<PackageReference Include="DKNet.EfCore.Hooks" Version="10.1.19" />
<PackageVersion Include="DKNet.EfCore.Hooks" Version="10.1.19" />
<PackageReference Include="DKNet.EfCore.Hooks" />
paket add DKNet.EfCore.Hooks --version 10.1.19
#r "nuget: DKNet.EfCore.Hooks, 10.1.19"
#:package DKNet.EfCore.Hooks@10.1.19
#addin nuget:?package=DKNet.EfCore.Hooks&version=10.1.19
#tool nuget:?package=DKNet.EfCore.Hooks&version=10.1.19
DKNet.EfCore.Hooks
A pluggable before/after-SaveChanges interceptor pipeline for EF Core. Implement a small hook interface instead of overriding SaveChangesAsync, and let independent concerns (audit stamping, domain events, data ownership, …) share the same DbContext without knowing about each other.
Installation
dotnet add package DKNet.EfCore.Hooks
Features
IBeforeSaveHookAsync/IAfterSaveHookAsync/IHookAsync— implement the phase(s) you need against a sharedSnapshotContextof changed entitiesHookAsyncbase class with no-op virtual methods to override only what you useAddDbContextWithHook<TDbContext>— registers theDbContextwith the hook interceptor wired inAddHook<TDbContext, THook>()— register a hook perDbContexttype; idempotent, inherited by derivedDbContexttypesdbContext.DisableHooks()— reference-counted, disposable scope to suppress all hooks (e.g. during seeding/migrations)- Used internally by
DKNet.EfCore.Events,DKNet.EfCore.AuditLogs, andDKNet.EfCore.DataAuthorization
Quick start
using DKNet.EfCore.Hooks;
using DKNet.EfCore.Extensions.Snapshots;
using Microsoft.EntityFrameworkCore;
public sealed class AuditStampHook(ICurrentUserService currentUser) : IBeforeSaveHookAsync
{
public Task BeforeSaveAsync(SnapshotContext context, CancellationToken cancellationToken = default)
{
foreach (var entry in context.Entities)
{
if (entry.OriginalState != EntityState.Added || entry.Entity is not IAuditedProperties)
continue;
// IAuditedProperties exposes get-only properties by design; write through the tracked entry.
entry.Entry.Property(nameof(IAuditedProperties.CreatedBy)).CurrentValue = currentUser.UserId;
entry.Entry.Property(nameof(IAuditedProperties.CreatedOn)).CurrentValue = DateTimeOffset.UtcNow;
}
return Task.CompletedTask;
}
}
// Registration
services.AddDbContextWithHook<AppDbContext>((provider, options) =>
options.UseSqlServer(connectionString));
services.AddHook<AppDbContext, AuditStampHook>();
Customisation reference
There is no options object — behaviour is entirely a function of what you register.
| Knob | Default | How to change it |
|---|---|---|
Which DbContext types run hooks |
none until registered | AddDbContextWithHook<TDbContext>(...), or options.UseHooks<TDbContext>(provider) when you register the context yourself |
Which hooks run for a DbContext |
none | AddHook<TDbContext, THook>(), once per (TDbContext, THook) pair; a repeat registration is a no-op |
| Hook execution order | DI registration order, all before-hooks then all after-hooks | register the hooks in the order you need |
| Hook lifetime | scoped, keyed by the DbContext type's full name |
not configurable |
HookRunnerInterceptor lifetime |
singleton, keyed by the DbContext type's full name |
not configurable |
AddDbContextWithHook — contextLifetime |
ServiceLifetime.Scoped |
pass a different ServiceLifetime |
AddDbContextWithHook — optionLifetime |
ServiceLifetime.Scoped |
pass a different ServiceLifetime |
| Hooks enabled | enabled | using (dbContext.DisableHooks()) { … } — reference-counted per DbContext CLR type, so nested scopes are safe |
Hooks are skipped for a save when the context is inside a DisableHooks() scope, or when the snapshot contains no
Added/Modified/Deleted entries. After-save hooks never run when the save itself threw.
Full documentation: https://github.com/baoduy/DKNet/blob/main/docs/EfCore/DKNet.EfCore.Hooks.md
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- DKNet.EfCore.Extensions (>= 10.1.19)
- DKNet.Fw.Extensions (>= 10.1.19)
- Microsoft.EntityFrameworkCore (>= 10.0.11)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on DKNet.EfCore.Hooks:
| Package | Downloads |
|---|---|
|
DKNet.EfCore.Events
DKNet is an enterprise-grade .NET library collection focused on advanced EF Core extensions, dynamic predicate building, and the Specification pattern. It provides production-ready tools for building robust, type-safe, and testable data access layers, including dynamic LINQ support, LinqKit integration. Designed for modern cloud-native applications, DKNet enforces strict code quality, async best practices, and full documentation for all public APIs. Enterprise-grade .NET library suite for modern application development, featuring advanced EF Core extensions (dynamic predicates, specifications, LinqKit), robust Domain-Driven Design (DDD) patterns, and domain event support. DKNet empowers scalable, maintainable, and testable solutions with type-safe validation, async/await, XML documentation, and high code quality standards. Ideal for cloud-native, microservices, and enterprise architectures. |
|
|
DKNet.EfCore.DataAuthorization
DKNet is an enterprise-grade .NET library collection focused on advanced EF Core extensions, dynamic predicate building, and the Specification pattern. It provides production-ready tools for building robust, type-safe, and testable data access layers, including dynamic LINQ support, LinqKit integration. Designed for modern cloud-native applications, DKNet enforces strict code quality, async best practices, and full documentation for all public APIs. Enterprise-grade .NET library suite for modern application development, featuring advanced EF Core extensions (dynamic predicates, specifications, LinqKit), robust Domain-Driven Design (DDD) patterns, and domain event support. DKNet empowers scalable, maintainable, and testable solutions with type-safe validation, async/await, XML documentation, and high code quality standards. Ideal for cloud-native, microservices, and enterprise architectures. |
|
|
DKNet.EfCore.AuditLogs
DKNet is an enterprise-grade .NET library collection focused on advanced EF Core extensions, dynamic predicate building, and the Specification pattern. It provides production-ready tools for building robust, type-safe, and testable data access layers, including dynamic LINQ support, LinqKit integration. Designed for modern cloud-native applications, DKNet enforces strict code quality, async best practices, and full documentation for all public APIs. Enterprise-grade .NET library suite for modern application development, featuring advanced EF Core extensions (dynamic predicates, specifications, LinqKit), robust Domain-Driven Design (DDD) patterns, and domain event support. DKNet empowers scalable, maintainable, and testable solutions with type-safe validation, async/await, XML documentation, and high code quality standards. Ideal for cloud-native, microservices, and enterprise architectures. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.1.19 | 0 | 9/3/2026 |
| 10.1.18 | 0 | 9/3/2026 |
| 10.1.17 | 0 | 9/3/2026 |
| 10.1.16 | 0 | 9/3/2026 |
| 10.1.15 | 93 | 9/1/2026 |
| 10.1.14 | 89 | 9/1/2026 |
| 10.1.13 | 108 | 8/31/2026 |
| 10.1.12 | 213 | 8/25/2026 |
| 10.1.11 | 147 | 8/24/2026 |
| 10.1.10 | 160 | 8/22/2026 |
| 10.1.9 | 188 | 8/22/2026 |
| 10.1.8 | 148 | 8/21/2026 |
| 10.1.7 | 160 | 8/21/2026 |
| 10.1.6 | 156 | 8/21/2026 |
| 10.1.5 | 233 | 8/20/2026 |
| 10.1.4 | 155 | 8/20/2026 |
| 10.1.3 | 169 | 8/19/2026 |
| 10.1.2 | 155 | 8/19/2026 |
| 10.1.1 | 142 | 8/19/2026 |
| 10.0.36 | 137 | 8/18/2026 |