Entity Framework 6: audit/track changes(实体框架 6:审计/跟踪更改)
问题描述
我的核心项目是用 C# 编写的.
I have my core project in C#.
我在一个数据库上工作,其中一些表具有user_mod"和date_mod"列,用于标记谁以及何时制作了一些 mod,而data_new"和user_new"也是如此.
I work on a database, where some tables have the columns "user_mod" and "date_mod" for sign who and when made some mods and the same with "data_new" and "user_new".
我的问题:有没有办法集中这个并自动插入这些数据,我在其中创建 dbContext
的实例?
My question: is there a way to centralize this and make this data inserted automatically, where I create the instance of dbContext
?
如果没有,我将使用审计跟踪工具.我见过其中的一些,但有一个问题:所有这些都需要我的模型中的一些代码.但我不想写在我的模型中,因为如果我必须更改它,我会丢失 mods.是否可以在不写入模型文件的情况下对 EF6 使用审计跟踪?怎么样?
If not, I will use an audit trail tool. I have seen some of these, but there is a problem: all of these, require some code in my model. But I don't want to write in my model, because if I have to change it, I will lost the mods. Is it possible use an audit trail for EF6 without writing in the model file(s)? How?
我尝试覆盖 saveChanges.
My attempt to override the saveChanges.
public partial class PieEntities : DbContext
{
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
var timestamp = DateTime.Now;
EntityState es = EntityState.Added;
ObjectStateManager o = new ObjectStateManager();
foreach (ObjectStateEntry entry in o.GetObjectStateEntries(EntityState.Added )) {
if (entry.Entity.GetType() == typeof(TabImpianti)) {
TabImpianti impianto = entry.Entity as TabImpianti;
impianto.DATA_INS = timestamp;
impianto.DATA_MOD = timestamp;
string u = mdlImpostazioni.p.UserName;
impianto.USER_INS = u;
impianto.USER_MOD = u;
}
}
return base.SaveChanges(options);
}
}
更新:我在这里总结了解决方案.
推荐答案
如果使用 EF6 的 DbContext
你可以在 SaveChanges
中使用 ChangeTracker
覆盖到查找添加/修改的自定义类型实体,例如 IAuditedEntity.
If using EF6's DbContext
you can use ChangeTracker
in SaveChanges
override to find added/modified entities of custom type, for example IAuditedEntity.
public interface IAuditedEntity {
string CreatedBy { get; set; }
DateTime CreatedAt { get; set; }
string LastModifiedBy { get; set; }
DateTime LastModifiedAt { get; set; }
}
public override int SaveChanges() {
var addedAuditedEntities = ChangeTracker.Entries<IAuditedEntity>()
.Where(p => p.State == EntityState.Added)
.Select(p => p.Entity);
var modifiedAuditedEntities = ChangeTracker.Entries<IAuditedEntity>()
.Where(p => p.State == EntityState.Modified)
.Select(p => p.Entity);
var now = DateTime.UtcNow;
foreach (var added in addedAuditedEntities) {
added.CreatedAt = now;
added.LastModifiedAt = now;
}
foreach (var modified in modifiedAuditedEntities) {
modified.LastModifiedAt = now;
}
return base.SaveChanges();
}
这篇关于实体框架 6:审计/跟踪更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实体框架 6:审计/跟踪更改
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30