7

I'm trying to attach an audit log to an entity I've written, I'm wondering if there are hooks into a context that I can override to provide the desired functionality.

What I'm looking to do is:

  • On Insert run method A
  • On Update run method B
  • On Delete run method C

I could manually add this in a controller but I'd rather a more concrete solution, the desired effect is that no method can insert into the table without also inserting into the audit log.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Valchris
  • 1,451
  • 2
  • 14
  • 33

1 Answers1

4

This project shows how you can add pre- and post-action hooks into an Entity Framework 4.1 DbContext. You can either extend its HooksDbContext class or see how it implements the hooks in the code and change it to suit your purposes.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 1
    Thanks, I took a look at the source code, it only applies context wide hooks which aren't exactly what I was looking for. I could do a more specific version of their implementation by overwriting the save changes method in my class.The disadvantage is that I don't like how that'd run this conditional code on all inserts for that context when it's only 1 Dbset I'd like this custom hook to apply too (unless I missed something). – Valchris Oct 04 '11 at 21:54