Dynamic audit table creation via Migrations

心已入冬 提交于 2019-12-11 00:59:52

问题


Using .Net Core 2.1 and Audit.NET EF 12.1.10, I'm trying to add a migration that includes the audit tables but when invoking Add-Migration, no audit tables are generated in the migration. I assumed that using the "dynamic" audit will do this automagically. I don't have any audit interfaces-- I am leaving this up to Audit.NET. Below is in my Startup:

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;
                }));

My DbContext extending from AuditIdentityDbContext:

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

I only have one entity so far, called Activity, just to test this out and I would expect Add-Migrations to include an Audit_Activity table as well as the Activity table, but I only got the latter. Not sure what I'm doing wrong here.

来源:https://stackoverflow.com/questions/51469506/dynamic-audit-table-creation-via-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!