通过迁移动态创建审计表

时间:2018-07-22 21:45:31

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

使用.Net Core 2.1和Audit.NET EF 12.1.10,我试图添加一个包含审核表的迁移,但是在调用“添加迁移”时,该迁移中没有生成审核表。我假设使用“动态”审核将自动完成此操作。我没有任何审核界面-我将其留给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>

到目前为止,我只有一个实体称为“活动”,只是为了对其进行测试,我希望“添加迁移”既包括Audit_Activity表也包括“活动”表,但是我只有后者。不知道我在做什么错。

0 个答案:

没有答案