I want to do the audit tables for my entities. At first I thought about overwriting the SaveChanges
method. I also thought about writing a method that will be doing the audit on my% base%. Using both ways, I will have a single auditing table for my entire system.
But for performance issues I need each entity to have its log table.
What would be the best way to intercept the type of my entity so I can save it in the audit table of it?
My code so far, looks like this:
public override int SaveChanges()
{
var entriesAdicionadas = ChangeTracker.Entries().Where(e => e.State == EntityState.Added).ToList();
var entriesModificadas = ChangeTracker.Entries().Where(e => e.State == EntityState.Modified).ToList();
var entriesDeletadas = ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted).ToList();
foreach (var entry in entriesAdicionadas)
{
}
int result = base.SaveChanges();
foreach (var entry in entriesModificadas)
{
var nome = entry.Entity.GetType().Name;
}
base.SaveChanges();
foreach (var entry in entriesDeletadas)
{
var nome = entry.Entity.GetType().Name;
}
return result;
}