2

I am using Audit.Net with Entity Framework Data Provider.

I am using one database for the application data and a different database (and DbContext) for the audits.

In the core configuration, I am seetting the the explicit mapper as in this example:

config.AuditTypeExplicitMapper(m =>
{
    m.Map<Client, ClientAudit>();
    m.AuditEntityAction<IAuditEntity>(SetAuditEntity);
});

Everything works fine. However later on I would like to load an entity and then see all audits for that entity.

What I easily do is query the Audit context with the Id of the entity. But then I need to know before hand the mappings from my Application context to my Audit context.

What I mean is. Find that for the Client entity, the ClientAudit entity is used. Does Audit.Net offers this possibility?

I tried looking directly in the Audit.Net code and documentation, but I found nothing that would give me this direct result.

Searching online I found no similar information. Seems I am the first to try to accomplish this.

Adam
  • 348
  • 2
  • 8
  • It looks like `IAuditEntityMapping` doesn't expose any accessors to read the mappings you've set so your best bet might be to persist those mappings yourself (maybe in your own config). – HasaniH Jul 04 '23 at 14:47

1 Answers1

0

If you use the Explicit Mapper, you can get the mapped type by using the AuditTypeMapper property in the EF data provider, for example:

var dataProvider = (EntityFrameworkDataProvider)Audit.Core.Configuration.DataProvider;
var auditType = dataProvider.AuditTypeMapper.Invoke(typeof(Client), null);
thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • This works perfectly. Thank you! – Adam Jul 05 '23 at 08:02
  • I also wonder if there is a neat way to get all audits related to a specific entity? – Adam Jul 05 '23 at 09:24
  • I don't understand what you mean by a neat way. A query to the database should not be complicated. Anyway, you can create a discussion [here](https://github.com/thepirat000/Audit.NET/discussions) – thepirat000 Jul 05 '23 at 17:04