问题
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