【发布时间】:2020-12-01 03:43:01
【问题描述】:
尝试在使用 .Net Core 和 EF Core 的项目中实施 Audit.Net
示例上下文定义为
[AuditDbContext(Mode = AuditOptionMode.OptOut, IncludeEntityObjects = true, AuditEventType = "{database}_{context}")]
public class MyContext : AuditDbContext
{....
主方法有这个启动代码
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<MyContext>();
DbInitializer.Initialize(context);
Audit.EntityFramework.Configuration.Setup()
.ForContext<MyContext>(config => config
.IncludeEntityObjects()
.AuditEventType("{context}:{database}"))
.UseOptOut()
.IgnoreAny(t => t.Name.EndsWith("History"));
}....
那么更新代码就是
var personToUpdate = await _context.People.FirstOrDefaultAsync(p => p.ID == id);
using (var audit = AuditScope.Create("Person:Update", () => personToUpdate ))
{
personToUpdate.FirstName = "audited";
_context.SaveChanges();
}
保存更改后,auditscope.event.target.new 为空
不太确定我错过了什么
【问题讨论】: