1

I'm having doubts about how to implement user code in the audit table. The audit.net includes the name of the user connected to the database (example: sa) but I would also like to include the user ID of my system. I ran some tests reading the code and I didn't succeed. Can you show me where I'm wrong, please?

//public partial class coletasEntities : DbContext (orignal)
public partial class coletasEntities : AuditDbContext (changed)
{
...
}

//my main form_load
 private void frmPrincipal_Load(object sender, EventArgs e)
    {
        Audit.Core.Configuration.Setup()
            .UseEntityFramework(x => x
            .AuditTypeNameMapper(typeName => "_Audit" + typeName)
            .AuditEntityAction<IAudit>((ev, ent, auditEntity) =>
            {
                auditEntity.AuditUser = Global.id_usuario; //my user ID
            }));

....
}

//interface create do add my custom property
public interface IAudit
{
     int AuditUser { get; set; }
}

If I just change the inheritance class from DbContext to Auditdbcontext the automatic generation for each audited table occurs but the custom field I want (obviously) is not included.

With the code modified as above is not created anything, nothing happens.

I'm using Entity Framework 6.

  • If the code inside `AuditEntityAction` is never reached, it could be because your audit entity `_AuditXXXXX` is not implementing the `IAudit` interface. – thepirat000 Jan 17 '19 at 23:31
  • But would the audit.net not have to create the audit tables automatically? – Willian Lopes Jan 18 '19 at 11:54
  • No it won't, you need to create the audit tables and specify the mapping on the configuration, also your audit entities should implement the `IAudit` if you want to use the generic `AuditEntityAction` – thepirat000 Jan 18 '19 at 17:13

0 Answers0