【发布时间】:2018-12-30 08:34:48
【问题描述】:
使用 .Net Core 2.1 和 Audit.NET EF 12.1.10,我正在尝试添加包含审计表的迁移,但是在调用 Add-Migration 时,迁移中不会生成审计表。我假设使用“动态”审计会自动执行此操作。我没有任何审计接口——我将把它留给 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 表,但我只得到了后者。不知道我在这里做错了什么。
【问题讨论】:
-
该库不提供任何自定义迁移。如果您在模型中包含审计表,则正常的 EF 迁移流程应该可以正常工作
标签: entity-framework-core-2.1 audit.net