views:

19

answers:

1

Hi all,

i have an audit table in SQL server 2008 database which keeps track of all changes made in tables. It does a great job, except it can't catch username of the currently logged user, because my connection string uses one username for all connections.

This app is written in ASP.NET MVC, this is why i have username stored in connection string.

Question is: how can i pass a user name to this table AFTER i call SaveChanges function?

+1  A: 

Don't use SaveChanges directly. Create own method (can be extension method) which will call SaveChanges and than run your additional code. Use this method every time you want to save changes.

The bad thing is that you can forget to call your method and use SaveChanges directly. To deal with it you have to refactor your code and hide ObjectContext to some wrapper - Repository. Expose Save method on repository and implement your save logic there.

Ladislav Mrnka
Ladislav, thanks for suggestion, i will try it out some time next week
Sasha