Swevo.EFCore.SoftDelete
1.0.1
Prefix Reserved
dotnet add package Swevo.EFCore.SoftDelete --version 1.0.1
NuGet\Install-Package Swevo.EFCore.SoftDelete -Version 1.0.1
<PackageReference Include="Swevo.EFCore.SoftDelete" Version="1.0.1" />
<PackageVersion Include="Swevo.EFCore.SoftDelete" Version="1.0.1" />
<PackageReference Include="Swevo.EFCore.SoftDelete" />
paket add Swevo.EFCore.SoftDelete --version 1.0.1
#r "nuget: Swevo.EFCore.SoftDelete, 1.0.1"
#:package Swevo.EFCore.SoftDelete@1.0.1
#addin nuget:?package=Swevo.EFCore.SoftDelete&version=1.0.1
#tool nuget:?package=Swevo.EFCore.SoftDelete&version=1.0.1
Swevo.EFCore.SoftDelete
Compile-time soft-delete generation for EF Core entities using Roslyn source generators. Add [SoftDelete] to any partial entity class and get IsDeleted + DeletedAt fields, an interceptor that converts hard deletes to soft deletes, and a global query filter — all at build time. Zero reflection, AOT-safe, no runtime overhead.
Installation
dotnet add package Swevo.EFCore.SoftDelete
Requires EF Core 7+.
Quick Start
1. Mark your entity
using EFCore.SoftDelete;
[SoftDelete]
public partial class Article
{
public int Id { get; set; }
public string? Title { get; set; }
}
The generator adds these automatically:
// Generated:
partial class Article : ISoftDeleteEntity
{
public bool IsDeleted { get; set; }
public DateTimeOffset? DeletedAt { get; set; }
}
2. Register the interceptor and query filter
// Program.cs
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(connectionString);
options.AddSoftDeleteInterceptor(); // converts Remove() → soft delete
});
// AppDbContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.AddSoftDeleteQueryFilters(); // hides soft-deleted records
}
3. Use normally
// Soft delete — entity stays in the database with IsDeleted = true
dbContext.Articles.Remove(article);
await dbContext.SaveChangesAsync();
// Queries automatically exclude soft-deleted records
var articles = await dbContext.Articles.ToListAsync(); // only active records
// Include soft-deleted when needed
var all = await dbContext.Articles.IgnoreQueryFilters().ToListAsync();
How It Works
| Operation | Behaviour |
|---|---|
dbContext.Remove(entity) |
Sets IsDeleted = true, DeletedAt = UtcNow — row stays in DB |
dbContext.Set<T>().ToListAsync() |
Excludes soft-deleted records (global query filter) |
dbContext.Set<T>().IgnoreQueryFilters() |
Includes soft-deleted records |
Non-[SoftDelete] entities |
Unaffected — hard-deleted as normal |
Generated Types
Emitted into your project's EFCore.SoftDelete namespace:
| Type | Description |
|---|---|
[SoftDelete] |
Attribute to mark entities |
ISoftDeleteEntity |
Interface with IsDeleted + DeletedAt |
SoftDeleteInterceptor |
SaveChangesInterceptor — converts deletes to soft deletes |
SoftDeleteInterceptorExtensions |
AddSoftDeleteInterceptor() on DbContextOptionsBuilder |
SoftDeleteModelBuilderExtensions |
AddSoftDeleteQueryFilters() on ModelBuilder |
EF Core Trilogy
Use alongside the other Swevo EF Core packages for a complete entity pipeline:
[Auditable] // Swevo.AutoAudit — CreatedAt, UpdatedAt, CreatedBy, UpdatedBy
[SoftDelete] // Swevo.EFCore.SoftDelete — IsDeleted, DeletedAt
public partial class Article
{
public int Id { get; set; }
// ...
}
Diagnostics
| ID | Severity | Description |
|---|---|---|
SDEL001 |
Error | Class must be partial to use [SoftDelete] |
Compatibility
| Dependency | Version |
|---|---|
| EF Core | 7.0+ |
| .NET | net7.0+ |
| C# | 10+ |
License
MIT © 2025 Justin Bannister
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.0: Initial release. Generates IsDeleted and DeletedAt fields, SoftDeleteInterceptor, and global query filter support.