I would like to create a Audit Trail entity in my database. The idea to keep a track of all the inserts, updates and deletes in tables. This table will have columns like Tablename, fieldname, recordid, beforevalue, aftervalue, datechanged, changedby, action(insert,update,delete).
Actually i was searching on this topic “Audit trial with LINQ” and it should happen on db.SubmitChanges() method action. I was expecting some features from LINQ to have this option by default in the latest version. I got tired searching lot of stuff googling and finally i got a blog where one has written a audit trial with LINQ.
Big thanks to Matt for his contribution !
matt’s objectives :
Automatic auditing of all inserts/updates/deletes for any table in your database with a single line of code, including:
- What table was modified?
- What fields changed?
- Who made the change?
- When did it occur?
He created a ER model like this :
Usage : Simply define your audit definitions at any time before calling SubmitChanges();
3: this.Orders.Audit().AuditAssociation(o => o.Order_Details);
4: this.Contacts.Audit().AuditAssociation(c => c.Addresses).AuditAssociation(c => c.PhoneNumbers);