Implementing “Triggers” and Soft Delete in FreeSQL
Introduction Recently I started a new project, tech stack AspNetCore + FreeSQL This ORM is really great to use, and the documentation is quite complete too. Here I’ll record two feature implementations related to “triggers” Recording update time when modifying an entity Model code All of my models are derived from this ModelBase, which comes with three property fields public abstract class ModelBase : ISoftDelete { public DateTime CreatedT
目录4 个章节
Introduction
Recently I started a new project, tech stack AspNetCore + FreeSQL
This ORM is really great to use, and the documentation is quite complete too. Here I’ll record two feature implementations related to “triggers”
Recording update time when modifying an entity
Model code
All of my models are derived from this ModelBase, which comes with three property fields
public abstract class ModelBase : ISoftDelete {
public DateTime CreatedTime { get; set; } = DateTime.Now;
public DateTime ModifiedTime { get; set; } = DateTime.Now;
public bool IsDeleted { get; set; } = false;
}
Now I need to automatically update the ModifiedTime field whenever each entity is modified
You can use the “audit property values” feature in the AOP provided by FreeSQL
According to the official introduction: this feature can uniformly handle certain values on insert/update, such as a snowflake-algorithm value for a property, a created-time value, or even a business value.
Configure it when creating the FreeSQL instance
Check the entity type; if it is a ModelBase type, then set the ModifiedTime field to the current modification time
// 实体变化事件监听
freeSql.Aop.AuditValue += (sender, args) => {
if (args.Object is ModelBase modelBase) {
modelBase.ModifiedTime = DateTime.Now;
}
};
Besides that, there’s another feature for monitoring entity modifications. I used this one at first, but found that it only executes after the entity has already been modified and saved, so it can’t implement the feature of recording the update time. Still, it’s quite useful for auditing, so I’ll note it down~
fsql.Aop.CurdAfter += (s, e) => {
Console.WriteLine($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId};"+
$" FullName:{e.EntityType.FullName} ElapsedMilliseconds:{e.ElapsedMilliseconds}ms, {e.Sql}");
};
Implementing soft delete
Current designs generally use soft delete to improve fault tolerance, so that data can still be recovered after accidental deletion.
The DjangoStarter framework I previously wrapped implemented soft delete itself (by overriding DjangoORM’s objects manager)
Coming to FreeSQL, according to the official documentation, you can implement soft delete directly using the filter feature.
First define an interface
public interface ISoftDelete {
bool IsDeleted { get; set; }
}
Actually, using the ModelBase above would also work
But I saw the official docs used it this way, so I followed along~ (interfaces are more generic; some entities may not need created-time and modified-time fields)
Then configure it when creating the FreeSQL instance
freeSql.GlobalFilter.Apply<ISoftDelete>("IsDeleted", a => a.IsDeleted == false);
This way, when reading data, items whose IsDeleted field is true are automatically filtered out
When deleting, don’t execute repo.Delete directly; instead set the IsDeleted field to true
Of course, you can also intercept all delete operations the same way as the AOP above.
References
- FreeSQL Docs - AOP - https://freesql.net/guide/aop.html
- FreeSQL Docs - Filters - https://freesql.net/guide/filters.html
_EOF_
Implementing “Triggers” and Soft Delete in FreeSQL
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
2024年我最喜欢的轻薄本模具,机械革命无界14X暴风雪开箱
鸡哥这个无界 14x 换了新的模具之后,可以算得上是我最的轻薄本模具了,1.46kg 的重量,价格与能耗比都最适合轻薄本的 8845 处理器,80Wh大电池,还有双 M2/双内存插槽,扩展性拉满,接口也非常丰富,直接就是轻薄本的高分答卷~ (为啥其他厂商不抄一波呢?)
2025-10-28 · 笔记本电脑 · 约 1 分钟
Asp-Net-Core学习笔记:身份认证入门
前言 过年前我又来更新了~ 我就说了最近不是在偷懒吧,其实这段时间还是有积累一些东西的,不过还没去整理…… 所以只能发以前没写完的一些笔记出来 就当做是温习一下啦 PS:之前说的红包封面我还没搞,得抓紧时间了 最近在准备搞一个我之前做的开源项目代码合集来做一期分享 两种常见的认证方式 先来看看两种常见的认证方式:基于token的认证和传统的session认证的区别。 session认证 我们知道,
2022-02-16 · Asp-Net-Core学习笔记 · 约 1 分钟
ChatGLM-6B本地部署
下载项目代码 git clone https://github.com/THUDM/ChatGLM-6B.git 创建虚拟环境,建议使用 conda 管理 conda create -n chatglm python==3.8 安装依赖 conda activate chatglm pip install -r requirements.txt conda install cudatoolkit=
2026-05-09 · LLM · 约 1 分钟
Linux下查看dd命令写入进度
前言 Linux下有一个强力工具,dd,用来操作镜像,简直神器,唯一的不足是没有显示操作进度,不过也不是没有办法,下面介绍几个查看写入进度的方法。 status选项查看进度 如果你使用的是GNU版本的dd,并且coreutils版本高于8.24,那么可以使用status选项。例如: sudo dd if=/dev/sda of=/dev/zero status=progress 上面命令在Mac
2025-10-27 · FAQ & Tips · 约 1 分钟
讨论
共 0 条加载评论中…