win7user10 / Laraue.EfCoreTriggers

Library to write triggers in C# with EF.Core
MIT License
114 stars 22 forks source link

Add automatic Auditing capability for different table #94

Open MitarNikolic opened 11 months ago

MitarNikolic commented 11 months ago

My issue here is that if I am trying to create triggers for auditing, I have to specify each column individually or execute a raw SQL query that looks something like this:

INSERT INTO auditing (SELECT * FROM source WHERE source.something = something)

The issue with this is that the auditing table has to look exactly the same as the source table. But that only allows insertion of one entry per element if you want to have the ID as a primary key. I would want to have an "AuditId" and a "SourceId" but that is not possible when using this query!

So I think it would be cool to have a method that you can use similarly to methods like Insert, Update, Upsertand so on called Audit

Would be cool if this could automatically map the columns with the same name and then you would have the ability to attach another method or perhaps a function argument that maps the rest of the values.

Usage:


 modelBuilder.Entity<Source>()
     .BeforeUpdate(trigger => trigger
         .Action(action => action
           //Does the automatic mapping
            .Audit<Source, Audit>()
            .HandleUnmapped( (source, audit) => audit.Action = "Update" )));