Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 356, comments - 173, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

Auditing with Entity Framework

 

I was investigating techniques to do auditing and think this is going to fit my needs, thought I would share. . . .

Let me start by saying there are about 1000 examples similar to this on the web. However none of the examples I found meet my specific requirements.

First lets discuss the requirements:

  1. I need to be able to apply auditing to only some of the types in my system
  2. I need to be able to ignore some of the properties on a type (i.e. not audit them)
  3. I need something flexible enough that I can apply it to multiple DbContexts
  4. I needed to have audit columns on each record (CreatedBy, CreatedOn, UpdatedOn, UpdatedBy).
  5. I needed a log table that would record when we got new records, when a field got changed, and when a record was deleted (including who and when it was done)

IAuditable Interface. I am using this guy to allow each type to “opt-in” to the auditing mechanism.

image

 

AuditIgnoreAttribute. I am using this to “opt-out” on a field by field basis of the auditing mechanism.

image

 

So now when I create my business object, I can mark him up appropriately depending on the requirements for that type. As you can see I want to audit my customer object, but not the Bio property.

image

 

Great now for the magic, the AuditableContext. This guy inherits from a normal DbContext, and overrides the SaveChanges method to add the auditing implementation. The auditing implementation is done in 5 steps:

  1. Figure out what the user changed and hang on to those changes for later
  2. Update the CreatedBy, CreatedOn, UpdatedOn, UpdatedBy columns as appropriate
  3. Perform a SaveChanges to write the users changes to the DB
  4. Update our list of user changes with the proper id’s (we need to do this because the data base is assigning the primary keys for our new entities)
  5. Save the list of user changes to the log table

image

image

 

That code should be straight forward. I did implement a helper method that figures out what fields we are interested by using a bit of reflection. NOTE: we are doing the reflection once per type and caching the results in a dictionary.

image

 

We only need to implement the AuditableContext once, each time we create a context in our app, we just inherit from it and we are off and to the races!

image

 

You can download a full working example from here

 

Happy Coding!

Print | posted on Tuesday, July 26, 2011 10:29 AM |

Feedback

Gravatar

# re: Auditing with Entity Framework

Awesome staff, thank you very much!!!
5/29/2012 3:29 AM | Alex
Gravatar

# re: Auditing with Entity Framework



using EF 5 with web api, I had to add this to the modified or the created kept getting overwritten
else if (entry.State == EntityState.Modified)
{
entry.Property(a => a.CreatedBy).IsModified = false;
entry.Property(a => a.CreatedOn).IsModified = false;
entry.Entity.LastModifiedOn = now;
entry.Entity.LastModifiedBy = user;
}
1/14/2013 12:01 PM | Teresa

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 7 and type the answer here:

Powered by: