2

I've been tasked with developing a means of auditing reads/edits/deletes of data from our databases. I did a search on how to do this with EF. I found this blog post on CodeProject, Implementing Audit Trail using Entity Framework Part-1. However, the article was written back in 2009. I'm sure it will work, but I'm wondering if using EF 6 and higher, there might be better ways of doing this? Does EF 6+ have a newer way of getting the necessary data for logging/auditing? Is there a "best practice" type of approach for auditing using EF 6+?

4
  • 2
    But why not just use database triggers? As an additional benefit it will log all actions performed on database directly, without EF.
    – Evk
    May 25, 2016 at 19:38
  • I've asked to use database triggers. I was told not to. That is why I'm having to seek alternative solutions.
    – Rod
    May 25, 2016 at 20:24
  • 1
    Hope they also provided some arguments in addition to that "no".
    – Evk
    May 25, 2016 at 20:37
  • Take a look at Audit.EntityFramework library. Sep 8, 2016 at 14:18

2 Answers 2

0

Well, there are some options, based on what your need is. I think auditing is best done on a database level due to performance and to audit changes made outside the entity framework context. Depending on the Sql Server version and edition you might be able to use build-in features like https://msdn.microsoft.com/en-us/library/cc280386.aspx

If you need to include application level data you might be able to include it in combination with using an interceptor (see https://msdn.microsoft.com/en-us/data/dn469464.aspx#BuildingBlocks.Result)

Another option that comes to mind is writing some auditing logic in an override of SaveChanges (see https://msdn.microsoft.com/en-us/library/cc716714(v=vs.100).aspx). This will only affect saves and not reads...

0

Auditing can still only be achieving by either implementing yourself or using a third-party library even for EF6 or EF Core.

Disclaimer: I'm the owner of the project Entity Framework Plus

This library support EF5, EF6 and EF Core and has multiple MUST-HAVE features like AutoSave in the Database.

// using Z.EntityFramework.Plus; // Don't forget to include this.

var ctx = new EntityContext();
// ... ctx changes ...

var audit = new Audit();
audit.CreatedBy = "ZZZ Projects"; // Optional
ctx.SaveChanges(audit);

// Access to all auditing information
var entries = audit.Entries;
foreach(var entry in entries)
{
    foreach(var property in entry.Properties)
    {
    }
}

Documentation: EF+ Audit

3
  • Jonathan, I think it's great that you answer questions like this. Also you always appropriately mention your personal interest (disclaimer). However, I think it would be better to mark questions like these as duplicate of a similar one that you answered before. You could choose one of your answers as a canonical answer that you can update whenever you think new information about your library is helpful. The duplicates will serve as signposts to this answer. May 25, 2016 at 20:11
  • 1
    Thank Gert for your feedback. I personally dislike “Duplicate” since they are never personalized to the current user/question, but I will try to mark them more often as duplicate to another of my answer. I will try to do as you mentioned unless the answer requires some code customization. May 25, 2016 at 22:08
  • Thanks! Yes, it's always a fine balance whether or not a question is different enough to merit its own answer. Maybe this one was - your first sentence answers what the OP actually asked. Anyway, four other votes are required before a question really goes down as duplicate, so it doesn't only depend on your opinion. May 25, 2016 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.