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; } = "";
}
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.
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.
Entities only after Initialize; otherwise it throws InvalidOperationException. Access after disposal throws ObjectDisposedException.