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
                    
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="Swevo.EFCore.SoftDelete" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Swevo.EFCore.SoftDelete" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Swevo.EFCore.SoftDelete" />
                    
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 Swevo.EFCore.SoftDelete --version 1.0.1
                    
#r "nuget: Swevo.EFCore.SoftDelete, 1.0.1"
                    
#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 Swevo.EFCore.SoftDelete@1.0.1
                    
#: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=Swevo.EFCore.SoftDelete&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Swevo.EFCore.SoftDelete&version=1.0.1
                    
Install as a Cake Tool

Swevo.EFCore.SoftDelete

NuGet Build License: MIT

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

There are no supported framework assets in this package.

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.

Version Downloads Last Updated
1.0.1 122 6/26/2026
1.0.0 98 6/26/2026

1.0.0: Initial release. Generates IsDeleted and DeletedAt fields, SoftDeleteInterceptor, and global query filter support.