简体   繁体   中英

Trigger stored procedure after SaveChanges in Entity Framework 1.0

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM