网站首页 > 技术教程 正文
描述
EFCore的触发器。在将更改提交到数据库之前和之后,响应DbContext中的更改。
你可以在使用EFCore将数据提交到数据库之前进行一些逻辑操作,同时在提交到数据库之后做业务逻辑的操作。
参数
来源:GitHub(264) https://github.com/koenbeuk/EntityFrameworkCore.Triggered
协议:MIT
文档:https://github.com/koenbeuk/EntityFrameworkCore.Triggered/wiki
示例
首先需要实现接口触发前和触发后的业务逻辑,你可以制定自己的业务逻辑
class StudentSignupTrigger : IBeforeSaveTrigger<Student> {
readonly ApplicationDbContext _applicationDbContext;
public class StudentTrigger(ApplicationDbContext applicationDbContext) {
_applicationDbContext = applicationDbContext;
}
public Task BeforeSave(ITriggerContext<Student> context, CancellationToken cancellationToken) {
if (context.ChangeType == ChangeType.Added){
_applicationDbContext.Emails.Add(new Email {
Student = context.Entity,
Title = "Welcome!";,
Body = "...."
});
}
return Task.CompletedTask;
}
}
class SendEmailTrigger : IAfterSaveTrigger<Email> {
readonly IEmailService _emailService;
readonly ApplicationDbContext _applicationDbContext;
public StudentTrigger (ApplicationDbContext applicationDbContext, IEmailService emailservice) {
_applicationDbContext = applicationDbContext;
_emailService = emailService;
}
public async Task AfterSave(ITriggerContext<Student> context, CancellationToken cancellationToken) {
if (context.Entity.SentDate == null && context.ChangeType != ChangeType.Deleted) {
await _emailService.Send(context.Enity);
context.Entity.SentDate = DateTime.Now;
await _applicationContext.SaveChangesAsync();
}
}
}
在DbContext中配置使用触发Triggers,代码如下
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<EmailService>();
services
.AddDbContext<ApplicationContext>(options => {
options.UseTriggers(triggerOptions => {
triggerOptions.AddTrigger<StudentSignupTrigger>();
triggerOptions.AddTrigger<SendEmailTrigger>();
});
})
.AddTransient<IEmailService, MyEmailService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ ... }
}
结尾
代码实现上还是比较简单易懂的,原理不难,感兴趣的可以自己研究代码。
如果你有审计日志的功能需求的,那么感觉使用这个库可以很方便的实现这个功能。
同时示例中带了审计功能的实现源码
猜你喜欢
- 2024-10-30 Spring Boot中RestTemplate开发实践(2)
- 2024-10-30 浅谈.NET EentityFramework的导航属性
- 2024-10-30 Entity Framework Core-删除数据(怎么删除framework4.0)
- 2024-10-30 EntityFramework Core 2.x/3.x (ef core) 在迁移中自动生成数据库表
- 2024-10-30 Entity Framework Core-使用Fluent API配置一对多关系
- 2024-10-30 .NET 7使用 Entity Framework Core 制作增删改查(CRUD) Web API 教程
- 2024-10-30 今日技术点:使用Entity Framework Core进行数据库迁移
- 2024-10-30 .Net Core下多种ORM框架特性及性能对比
- 2024-10-30 Entity Framework 7 支持批量操作和 JSON 列
- 2024-10-30 Entity Framework Core-Fluent API
你 发表评论:
欢迎- 最近发表
-
- linux CentOS检查见后门程序的shell
- 网络安全工程师演示:黑客是如何使用Nmap网络扫描工具的?
- Linux中ftp服务修改默认21端口等(linux修改ftp配置文件)
- Linux系统下使用Iptables配置端口转发,运维实战收藏!
- 谈谈TCP和UDP源端口的确定(tcp和udp的端口号相同吗)
- Linux 系统 通过端口号找到对应的服务及相应安装位置
- 快速查找NAS未占用端口!Docker端口秒级排查+可视化占坑双杀技
- 【知识杂谈#2】如何查看Linux的(本地与公网)IP地址与SSH端口号
- 如何在Linux中查询 DNS 记录,这三个命令可谓是最常用、最经典的
- 【Linux系统编程】特殊进程之守护进程
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)