3

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.

Los Morales
  • 2,061
  • 7
  • 26
  • 42
  • 1
    The library does not provide any custom migration. If you included the audit tables on your model, the normal EF migrations flow should work just fine – thepirat000 Jul 24 '18 at 20:04

1 Answers1

0

I tried Auditing Identity Roles just because it was easiest to test at the moment

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

Then I used your code in 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;
                }));

What I found out is that the Id had to be string for some reason, it could not be int.

Screenshot from the link shows that the changes in data have saved.

enter image description here

On the side note, I wanted to save the user logged in via Identity, so in case anyone wondering, this post helped me achieve it. https://entityframeworkcore.com/knowledge-base/49799223/asp-net-core-entity-changing-history

Murometz80
  • 21
  • 4