Delete, restore and audit fields

Hard or soft delete

[CrudPage("courses", Entity = typeof(Course))]                              // Auto: soft if Course has a flag
[CrudPage("memos",   Entity = typeof(Memo),  Delete = DeleteMode.Hard)]     // always removes the row
[CrudPage("drafts",  Entity = typeof(Draft), Delete = DeleteMode.Soft)]     // must have a flag

Auto (the default) is soft when the entity has a soft-delete flag, hard otherwise. App-wide: o.DefaultDeleteMode = DeleteMode.Hard.

The flag, found in this order:

  1. The entity implements ISoftDelete (or IDeletionAudited / IFullAudited) from Attritect.Contracts.
  2. It implements your own interface, mapped once: o.MapAuditFields<IMyAudited>(m => m.IsDeleted(x => x.Removed).DeletedAt(x => x.RemovedOn)).
  3. A bool IsDeleted property, any letter case (o.SoftDeletePropertyName to rename).

A property that looks like a misspelled flag (IsDelete) is a warning in the editor and at startup, never a silent hard delete.

What soft delete does

  • Delete sets the flag (and DeletedAt / DeletedBy when present). Deleting/Deleted events run as usual.
  • Hidden everywhere: lists, gets, lookups, reference checks, edits, exports.
  • Like a foreign key: a row other rows still use ([Lookup], [Parent]) can’t be deleted (409).
  • Unique values are freed: a new row may reuse them. Restoring a row whose value is now taken is a 409. Database unique indexes must ignore deleted rows too; ApplyCrudConventions does that for you (below).
  • Restore (POST {page}/deleted/{id}/restore) brings the row back with the same key.
  • Purge (DELETE {page}/deleted/{id}) removes it for good: host role or the exact crud:{page}:purge claim.
  • Restoring/Restored/Purging/Purged events, and behaviors, wrap these too.

Audit fields

using Attritect.Contracts;   // the Domain may reference only this package

public class Course : IFullAudited    // or IFullAudited<Guid?> for a foreign key to users
{
    public int Id { get; set; }
    public string Name { get; set; } = "";

    public bool IsDeleted { get; set; }
    public DateTime CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTime? ModifiedAt { get; set; }
    public string? ModifiedBy { get; set; }
    public DateTime? DeletedAt { get; set; }
    public string? DeletedBy { get; set; }
}
Interface Fields
ISoftDelete IsDeleted
ICreationAudited CreatedAt, CreatedBy
IModificationAudited ModifiedAt, ModifiedBy
IDeletionAudited ISoftDelete + DeletedAt, DeletedBy
IFullAudited all of them

Each has a generic form for the user key (ICreationAudited<int?>); the plain form uses string?. The user comes from ICrudUser (with AddHttpApi: the claim NameIdentifier, else sub, else the user name). An id that doesn’t fit the column (e.g. "alice" for int?) is stored as null.

Filled on every save by an EF Core interceptor that UseEntityFrameworkCore registers: engine writes and your own SaveChanges. Creation values can’t be changed later. In your own code, db.Remove(course) on a soft-delete entity becomes a soft delete; db.HardRemove(course) deletes for real. Times are UTC.

The EF filter: hide deleted rows in your own queries

Call this last in OnModelCreating:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
    modelBuilder.ApplyCrudConventions(this);
}
  • Adds the EF Core named query filter "SoftDelete" to soft-delete entities: your queries, Includes and navigation counts hide deleted rows. Switch it off with db.Courses.IgnoreQueryFilters(["SoftDelete"]).
  • Makes unique indexes ignore deleted rows.
  • Marks audit times as UTC when read (the API sends ...Z), and sizes the “by” columns.

SQL views must filter the flag themselves.

Restoring hard-deleted rows

With the pro Audit package, a hard-deleted row can be restored from its history; it comes back under its old key (ICrudService.CreateWithKeyAsync), or under a new one when the key is taken. SQL Server identity columns work too: the EF store switches IDENTITY_INSERT on for that one save.