在.Net核心Web应用程序中,我使用Audit.EntityFramework 16.1.1.net core 3.1来记录更改,用于两个目的:

  • 记录特定表的更改以用于历史/安全和
  • 将所有更改记录到一个表中(计划的作业(job)定期删除该表),以便在需要时进行一些基本的取证.

Startup.cs中使用的代码如下:

var currentUser = app.ApplicationServices.GetRequiredService<ICurrentUser>();

Audit.Core.Configuration.Setup()
    .UseEntityFramework(ef => ef
        .AuditTypeMapper(t => typeof(AuditLog))
        .AuditEntityAction<AuditLog>((ev, entry, entity) =>
        {
            entity.Id = Guid.NewGuid();
            entity.AuditData = entry.ToJson();
            entity.EntityType = entry.EntityType.Name;
            entity.AuditDate = DateTime.Now;
            entity.AuditUsername = currentUser.GetUsername();
            entity.AuditUserId = currentUser.GetUserId();
            entity.EntityId = entry.PrimaryKey.Count > 1 ?
                string.Join("|", entry.PrimaryKey.Select(pk => pk.Value)) :
                entry.PrimaryKey.First().Value.ToString();
            entity.Action = entry.Action;
        })
        .IgnoreMatchedProperties(true));

Audit.Core.Configuration.Setup()
    .UseEntityFramework(ef => ef
        .AuditTypeExplicitMapper(m => m
        .Map<Table1, Table1History>((table, tableHistory) =>
        {
            FillTableHistoryData(table, tableHistory);
        })
        .Map<Table2, Table2History>((otherTable, otherTableHistory) =>
        {
            FillOtherTableHistoryData(otherTable, otherTableHistory);
        })

在将应用程序升级到.net core 6Audit.EntityFramework19.1.0(或任何更新版本)后,这就停止了工作.返回到16.1.1.会抛出MethodNotFoundException(对于GetTableName方法).

所以,我想避免一些严重的重构,同时让它像以前一样工作.在这方面有经验的人能给我一个正确的方向吗?

推荐答案

我不知道为什么它在旧版本中有效,但你不应该调用 .UseEntityFramework()次,因为它每次都会覆盖设置.

一种 Select 是在AuditTypeExplicitMapper中使用.MapExplicit来处理.Map<>调用中不包含的表.

例如:

Audit.Core.Configuration.Setup()
    .UseEntityFramework(ef => ef
        .AuditTypeExplicitMapper(m => m
            .Map<Table1, Table1History>((table, tableHistory) =>
            {
                FillTableHistoryData(table, tableHistory);
            })
            .Map<Table2, Table2History>((otherTable, otherTableHistory) =>
            {
                FillOtherTableHistoryData(otherTable, otherTableHistory);
            })
            .MapExplicit<AuditLog>(entry => IsAnotherTable(entry), (entry, entity) =>
            {
                entity.Id = Guid.NewGuid();
                entity.AuditData = entry.ToJson();
                entity.EntityType = entry.EntityType.Name;
                // ...
            }))
        .IgnoreMatchedProperties(true));

您需要指定.MapExplicit将处理哪些实体,例如:

bool IsAnotherTable(EventEntry entry)
{
    return entry.GetEntry().Entity is not Table1 and not Table2;
}

Csharp相关问答推荐

如何使用Automapper映射两个嵌套列表

如何修改中间件或其注册以正确使用作用域服务?

Nuget包Serilog.Sinks.AwsCloudwatch引发TypeLoadExceptions,因为父类型是密封的

C#-从基类更新子类

如何将MongoDB序列化程序设置为内部对象属性

如何在C#中实现非抛出`MinBy`?

在EF Core中,有没有什么方法可以防止在查询中写入相同的条件,而代之以表达式?

如何使用EF Core和.NET 8来upsert到具有多对多关系的表?

如何通过寻找向量长度来优化两个循环?

为什么AggregateException的Catch块不足以处理取消?

ASP.NET Core MVC将值从视图传递到控制器时出现问题

如何管理Azure认证客户端响应和证书 fingerprint

无法将生产环境的AppDbContext设置替换为用于集成测试的内存数据库

实体框架-IsRequired()与OnDelete()

从Base64转换为不同的字符串返回相同的结果

如何在特定环境中运行dotnet测试?

在平行内使用跨度.用于循环

从GRPC连接创建ZipArchive

如何设置WinForms按钮焦点,使其看起来像是被Tab键插入其中?

如何通过WinSCP判断SFTP会话中使用的加密算法?