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:
- The entity implements
ISoftDelete(orIDeletionAudited/IFullAudited) fromAttritect.Contracts. - It implements your own interface, mapped once:
o.MapAuditFields<IMyAudited>(m => m.IsDeleted(x => x.Removed).DeletedAt(x => x.RemovedOn)). - A
bool IsDeletedproperty, any letter case (o.SoftDeletePropertyNameto 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/DeletedBywhen present).Deleting/Deletedevents 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;
ApplyCrudConventionsdoes 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 exactcrud:{page}:purgeclaim. Restoring/Restored/Purging/Purgedevents, 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 withdb.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.