GKit.EntityFramework 1.1.3

dotnet add package GKit.EntityFramework --version 1.1.3
                    
NuGet\Install-Package GKit.EntityFramework -Version 1.1.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GKit.EntityFramework" Version="1.1.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GKit.EntityFramework" Version="1.1.3" />
                    
Directory.Packages.props
<PackageReference Include="GKit.EntityFramework" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GKit.EntityFramework --version 1.1.3
                    
#r "nuget: GKit.EntityFramework, 1.1.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GKit.EntityFramework@1.1.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GKit.EntityFramework&version=1.1.3
                    
Install as a Cake Addin
#tool nuget:?package=GKit.EntityFramework&version=1.1.3
                    
Install as a Cake Tool

GKit.EntityFramework

Soft deletes and entity revisions for Entity Framework Core, applied through save interceptors.

Audit timestamps

Two timestamps are written by this package: a soft-deleted entity's DeletedAt, and a revision's RevisionInfo.CreatedAt.

Both are stamped in UTC by default. An application that configures nothing keeps exactly that behaviour — the clock below is an addition, not a change.

Supplying a clock

Either per interceptor:

var clock = new FakeTimeProvider(new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero));

var softDelete = new SoftDeleteInterceptor(clock);
var revision = new RevisionInterceptor(clock);

or once, for the application:

builder.Services.AddGKitEntityFramework();          // UTC, the default
builder.Services.AddGKitEntityFramework(myClock);   // everything stamped from myClock

The provider is taken as a parameter rather than resolved from the container. An application may register a TimeProvider for something unrelated, and picking that up here would quietly change what lands in audit columns.

Interceptors can still be constructed directly, which is what a context exposing them as property initialisers does:

public class MyContext(DbContextOptions options) : DbContext(options), ISoftDeleteAwareContext
{
    public SoftDeleteInterceptor SoftDeleteInterceptor { get; } = new();

    protected override void OnConfiguring(DbContextOptionsBuilder b)
        => b.AddInterceptors(SoftDeleteInterceptor);
}

The extension is a convenience over that, not a replacement for it.

Why UTC is the default, and why local time is an opt-in

UTC is unambiguous. Local time is not: on the autumn daylight-saving fall-back, 02:30 occurs twice in the same night, and a stored local timestamp cannot tell you which one it was. Rows written either side of that boundary also stop sorting correctly.

So supplying a provider that reports local time is a deliberate choice — reasonable if operators read these columns directly and the ambiguity is acceptable, and worth avoiding otherwise. The package writes whatever instant the clock reports and applies no conversion of its own.

One reading per save

Each interceptor reads its clock once per SaveChanges and stamps every affected entity from that single reading, so a save that deletes two rows — or deletes one and revises another — cannot produce timestamps that disagree.

Entities that are both revisionable and soft-deletable

An entity may implement both interfaces:

public class Note : IRevisionableEntity, ISoftDeletableEntity
{
    public int Id { get; set; }
    public string Text { get; set; } = "";
    public DateTime? DeletedAt { get; set; }
    public RevisionInfo Revision { get; set; } = RevisionInfo.First();

    public object Clone() => new Note { Text = Text, Revision = Revision.Clone() };
}

Deleting one marks it in place and creates no revision. The current revision gets DeletedAt, stays current, and the document's revision count is unchanged. A deletion is not an edit, so it does not produce a new revision the way a modification does.

The result does not depend on the order you register the interceptors. Both of these produce identical rows:

optionsBuilder.AddInterceptors(SoftDeleteInterceptor, RevisionInterceptor);
optionsBuilder.AddInterceptors(RevisionInterceptor, SoftDeleteInterceptor);

Earlier revisions are left alone. Deleting a document marks only the revision you deleted. Earlier revisions keep DeletedAt == null, so a query that does not filter IsCurrent still returns them — WithRevision adds no query filter, and filtering history is the application's call:

// The live document, if it has not been deleted.
var current = await ctx.Notes.SingleOrDefaultAsync(p => p.Revision.IsCurrent);

// Its history, including revisions of documents since deleted.
var history = await ctx.Notes.Where(p => !p.Revision.IsCurrent).ToListAsync();

Restoring is a change, and does produce a revision. Clearing DeletedAt on a marked entity creates a new revision, because the document coming back is part of its history.

Permanent removal is unaffected. ctx.Remove(entity, hard: true) removes the row and creates no revision, as it does for an entity that is only soft-deletable.

First revisions

RevisionInfo.First() leaves CreatedAt unset; RevisionInterceptor stamps it when the entity is saved. That is what brings first revisions within reach of a configured clock at all: First() is called from entity field initialisers, where no clock is in scope.

public RevisionInfo Revision { get; set; } = RevisionInfo.First();   // unchanged

Two consequences worth knowing:

  • An unsaved entity's CreatedAt reads as default until it is saved, where it previously read as the time the object was allocated.
  • Only a default value is stamped. If you assign a historical CreatedAt — importing existing data, say — it survives the save untouched.
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.3 98 11/4/2026
1.1.2 98 11/4/2026
1.0.0 132 9/23/2026
0.0.19 115 9/11/2026
0.0.18 117 8/31/2026
0.0.17 157 6/24/2026
0.0.16 120 5/2/2026
0.0.15 143 4/3/2026
0.0.14 131 3/26/2026
0.0.13 145 2/25/2026
0.0.12 149 1/30/2026
0.0.11 402 11/17/2025
0.0.10 258 10/28/2025
0.0.9 245 9/30/2025
0.0.8 297 9/14/2025
0.0.7 260 9/3/2025
0.0.6 190 7/11/2025
0.0.4 230 6/19/2025
0.0.3 333 5/13/2025
0.0.2 319 5/12/2025