dotnetcore / dotnetcore/FreeSql

How to intercept and modify Delete operations to Update for soft-delete pattern

Open
#2,035 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

## Description
I'm trying to implement a soft-delete pattern using FreeSql's AOP interceptors. When a delete operation is performed on an entity that inherits from `FullAuditedEntity`, I want to cancel the delete operation and instead perform an update operation to set `IsDeleted = true` and update other audit fields.

## Current Code
```csharp
fsql.Aop.CurdBefore += (s, e) =>
{
// How can I cancel this Curd and convert it to an Update operation?
if (e.CurdType == FreeSql.Aop.CurdType.Delete && typeof(FullAuditedEntity).IsAssignableFrom(e.EntityType))
{
// Need to:
// 1. Cancel the delete operation
// 2. Get the entity being deleted
// 3. Set IsDeleted = true and other audit fields
// 4. Perform an update instead

// I want to set these values:
// auditEntity.IsDeleted = true;
// auditEntity.DeletedTime = DateTime.Parse("2025-06-10 18:46:41"); // Current UTC time
// auditEntity.DeletedBy = "ErenKrt"; // Current user
}
};
```

## What I Need
I need to know:
1. How to cancel the current Delete operation from within `CurdBefore` event
2. How to access the entity or entities being deleted
3. How to perform an Update operation instead

## Environment
- FreeSql version: 3.5.207
- FreeSql.Repository: I'm using the FreeSql.Repository package with `AddFreeRepository();` for dependency injection
- .NET version: .NET 9.0
- Database provider: [MySQL/SQL Server/etc.]

## Additional Context
I'm implementing a soft-delete pattern where entities that inherit from `FullAuditedEntity` should never be physically deleted from the database. Instead, they should be marked as deleted by setting `IsDeleted = true` and updating audit fields like `DeletedTime` and `DeletedBy`.

I'm specifically using the FreeSql.Repository system with `AddFreeRepository();` for registering repositories, and I'm performing delete operations using `Repository.DeleteAsync()` method.

Thank you for your help!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.