开发者

Trigger stored procedure after SaveChanges in Entity Framework 1.0

开发者 https://www.devze.com 2023-01-15 06:58 出处:网络
i have an audit table in SQL server 2008 database which keeps track of all changes made in tables. It does a great job, e开发者_运维百科xcept it can\'t catch username of the currently logged user, bec

i have an audit table in SQL server 2008 database which keeps track of all changes made in tables. It does a great job, e开发者_运维百科xcept 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消