使用实体框架中的 ObjectContext.SaveChanges 方法有选择地审核日志记录

发布于 2024-11-14 11:51:05 字数 132 浏览 8 评论 0原文

我需要在应用程序中执行审核日志记录,并且希望 SaveChanges 记录对数据库的任何更改。在某些情况下,我不希望发生日志记录。告诉 SaveChanges 不执行审核日志记录的最优雅的方式是什么?我正在使用实体框架 4。

I need to perform audit logging in my application and I want SaveChanges to log any changes to the database. There are certain instances where I don't want the logging to occur. What would be the most elegant way of telling SaveChanges to not perform the audit logging? I'm using Entity Framework 4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-11-21 11:51:05

我将创建一个自定义属性来装饰需要日志的元素。它将通过 log 方法读取。此属性可以应用于类和属性,以便您可以精确选择应记录的内容。

我建议您使用缓存来避免每次都检查是否应该记录某些内容。

I would create an custom attribute to decorate the elements requiring log. It would be read by the log method. This attribute could be applied on class and properties so that you could select precisely what should be logged.

I suggest you use caching to avoid checking every time if something should be logged or not.

梨涡少年 2024-11-21 11:51:05

我已经实现了类似的日志记录要求。我将表的信息存储在数据库中名为 tablemaster 的元表中,并存储与登录相关的大量信息。其中一个字段指示是否记录该表的条目。
当在上下文中调用保存更改时,我会记录审核跟踪,即当我决定是否要对这些表进行日志记录时。
元表 tablemaster 在应用程序启动时被提取到内存中,因此无需往返检查详细信息。希望这有帮助。

I have implemented similar requirement for logging. I store tables' info in a metatable called tablemaster in database, and store a lot of information related to logging in that. One of the fields tells whether to log entries for that table or not.
I log Audit trail when saving changes is called in context, that is when I decide if logging for these tables is to be done or not.
Meta Table tablemaster is fetched in the memory at start of App, so no round trips for checking details. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文