DKNet

Capture a save-time snapshot for hooks

Part of DKNet.EfCore.Extensions. Keep the entries and their pre-save states available after EF Core has changed tracking state.

Use the console-project setup in the Quick Start, then replace Program.cs with this example.

using Microsoft.EntityFrameworkCore;
using DKNet.EfCore.Extensions.Snapshots;

using var connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source=:memory:");
connection.Open();
var o = new DbContextOptionsBuilder<DemoDb>();
o.UseSqlite(connection);
await using var db = new DemoDb(o.Options);
db.Add(new Item { Name = "Coffee" });
using var snapshot = new SnapshotContext(db);
snapshot.Initialize();
Console.WriteLine(snapshot.Entities.Single().OriginalState); // Added

public sealed class DemoDb(DbContextOptions<DemoDb> options) : DbContext(options)
{
    public DbSet<Item> Items => Set<Item>();
}
public sealed class Item
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
}

SnapshotContext.Initialize detects changes, captures Added/Modified/Deleted entries and stores each entry OriginalState for later hook phases.

SnapshotEntityEntry retains the live Entry and Entity references plus the copied OriginalState. Disposal clears the captured list and leaves the DbContext and its tracker settings untouched.

🧱 Where it fits

Hooks creates one SnapshotContext per save. EventHook, EfCoreAuditHook and DataOwnerHook inspect its entries through their hook callbacks; they do not each build a separate capture.

The package component map shows the host, contracts and consumers involved in this capability.

⚠️ Gotchas & limits