entity-framework-core-2.1 - 通过迁移创建动态审计表

标签 entity-framework-core-2.1 audit.net

使用 .Net Core 2.1 和 Audit.NET EF 12.1.10,我尝试添加一个包含审计表的迁移,但是在调用 Add-Migration 时,迁移中没有生成任何审计表。我假设使用“动态”审核会自动执行此操作。我没有任何审计接口(interface)——我将其留给 Audit.NET。以下是我的启动:

var serviceProvider = services.BuildServiceProvider();

        Audit.EntityFramework.Configuration.Setup()
            .ForContext<MainDbContext>(config => config
                .IncludeEntityObjects()
                .AuditEventType("{context}:{database}"))
            .UseOptOut()
            .IgnoreAny(entity => entity.Name.StartsWith("AspNet") && entity.Name.StartsWith("OI"));


        Audit.Core.Configuration.Setup()
            .UseEntityFramework(ef => ef
                .AuditTypeNameMapper(typeName => "Audit_" + typeName)
                .AuditEntityAction((evt, entry, auditEntity) =>
                {
                    // Get the current HttpContext 
                    var httpContext = serviceProvider.GetService<IHttpContextAccessor>().HttpContext;

                    // Store the identity name on the "UserName" property of the audit entity
                    ((dynamic)auditEntity).UserName = httpContext.User?.Identity.Name;
                    ((dynamic)auditEntity).AuditDate = DateTime.UtcNow;
                    ((dynamic)auditEntity).AuditAction = entry.Action;
                }));

我的 DbContext 从 AuditIdentityDbContext 扩展:

public class MainDbContext : AuditIdentityDbContext<User, Role, string>

到目前为止,我只有一个实体,称为 Activity,只是为了对此进行测试,我希望 Add-Migrations 包括 Audit_Activity 表和 Activity 表,但我只得到了后者。不确定我在这里做错了什么。

最佳答案

我尝试了审核身份角色只是因为目前它最容易测试

public class ApplicationRole : IdentityRole
{
}

public class Audit_ApplicationRole : IAudit
{
    [Key]
    public string Id { get; set; }
    [Column(TypeName = "NVARCHAR(256)")]
    public string Name { get; set; }
    [Column(TypeName = "NVARCHAR(256)")]
    public string NormalizedName { get; set; }
    public string ConcurrencyStamp { get; set; }
    public ApplicationRole Role { get; set; }
    public string RoleId  { get; set; }
    [Column(TypeName = "VARCHAR(100)")]
    public string AuditUser { get; set; }
    public DateTime AuditDate { get; set; }
    [Column(TypeName = "VARCHAR(7)")]
    public string Action { get; set; } // "Insert", "Update" or "Delete"
}

public interface IAudit
{
    string AuditUser { get; set; }
    DateTime AuditDate { get; set; }
    string Action { get; set; } 
}

然后我在 StartUp.cs 中使用了你的代码

        Audit.EntityFramework.Configuration.Setup()
            .ForContext<ApplicationDbContext>(config => config
                .IncludeEntityObjects()
                .AuditEventType("{context}:{database}"));

        Audit.Core.Configuration.Setup()
            .UseEntityFramework(x => x
                .AuditTypeNameMapper(typeName => "Audit_" + typeName)
                .AuditEntityAction<IAudit>((ev, ent, auditEntity) =>
                {
                    auditEntity.AuditDate = DateTime.UtcNow;
                    auditEntity.AuditUser = ev.Environment.UserName;
                    auditEntity.Action = ent.Action;
                }));

我发现由于某种原因,Id 必须是字符串,不能是 int。

链接的屏幕截图显示数据更改已保存。

enter image description here

在旁注中,我想保存通过身份登录的用户,所以如果有人想知道,这篇文章帮助我实现了它。 https://entityframeworkcore.com/knowledge-base/49799223/asp-net-core-entity-changing-history

关于entity-framework-core-2.1 - 通过迁移创建动态审计表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51469506/

相关文章:

c# - 如何通过事件或覆盖 SaveChangesAsync() 在 DbContext 中获取所有已保存的实体值

c# - Audit.Net,-即使抛出异常也需要正确的审计

.net-core - 如何通过 NLog 记录 Entity Framework Core 操作

entity-framework - 英孚核心 : is it possible to support two models in one database?

c# - 当我无权访问 HttpContext 时,如何填充我的审计日志 UserId 字段?

c# - DbContext的第三方实现,如何同时实现IdentityDbContext

entity-framework - 如何使用 Audit.Net 保存实体关系?

entity-framework-core - EF Core 迁移错误 : Database 'MyDatabaseName' already exists. 选择不同的数据库名称

entity-framework - 如何处理减慢构建和 IDE 的巨大 efcore 迁移设计器文件