LinqToSQL 和审核更改的字段

发布于 2024-07-18 21:53:42 字数 793 浏览 1 评论 0原文

这是另一个 LinqToSQL 问题,我确信我一定在某个地方错过了机会,因为 O/R 设计器的行为让我非常困惑......

我有一个用于 LinqToSQL 表的基类,我将其称为LinqedTable。 我已经成功地使用反射来获取后代类的所有属性并执行其他标准操作。

现在我想对我的表进行一些自动审核,以便每当插入或删除 LinqedTable 记录或字段值更改时,我都会将一条记录插入到审核表中,详细说明更改类型、字段名称及其保存前和保存后的值。

我认为我可以使用 PropertyChanging 事件来完成此操作,在保存之前跟踪所有更改的属性,然后在每次 SubmitChanges() 后清除更改集合称呼。 但是 - 由于某些奇怪的原因,O/R 设计器生成的代码不会在 PropertyChanging 事件中为您提供属性名称 - 它会发送一个空字符串! (为什么?!)它确实PropertyChanged 事件中发送属性名称,但这对我来说已经太晚了,无法获取原始值。

我想使用 OnLoaded() 部分方法获取所有属性的所有原始值 - 但根据定义,这是私有的,我需要访问基类中的该方法。 即使我使用反射来获取该方法,这也意味着我必须为每个表实现部分方法的另一半,这有点违背了继承的目的!

我也无法在 DataContext 中找到任何合适的方法来使用或覆盖。

那么,您建议如何让此审核功能发挥作用呢?

Here's another one of these LinqToSQL questions where I'm sure I must have missed the boat somewhere, because the behavior of the O/R Designer is very puzzling to me...

I have a base class for my LinqToSQL tables, which I called LinqedTable. I've successfully used reflection to get hold of all the properties of the descendant classes and do other standard stuff.

Now I want to have some automatic auditing of my tables, so that whenever a LinqedTable record is inserted or deleted, or a field value changes, I will insert a record into an audit table, detailing the change type, the field name, and its value pre- and post-save.

I thought I would be able to do it using the PropertyChanging event, keeping track of all the changed properties before a save, then clearing the collection of changes after each SubmitChanges() call. But - the generated code from the O/R designer, for some bizarre reason, doesn't give you the property name in the PropertyChanging event - it sends an empty string! (WHY?!) It does send the property name in the PropertyChanged event, but that's already too late for me to get the original value.

I thought to grab all the original values of all properties using the OnLoaded() partial method - but that is private by definition, and I need access to that method in the base class. Even if I used reflection to get hold of that method, that would mean I would have to implement the other half of the partial method for every one of my tables, which kinda defeats the purpose of having inheritance!

I also can't find any suitable method in the DataContext to use or override.

So what would you recommend to get this audit functionality working?

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

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

发布评论

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

评论(2

无名指的心愿 2024-07-25 21:53:42

您可以在 DataContext 上使用 GetChangeSet 来检索上下文中所有表上发生的更新、插入和删除的列表。 您可以使用 ITable.GetOriginalEntityState 检索已更改实体的原始值。 但是,当您检索已删除或更新的记录的原始值时,关联将不可用,因此如果您需要处理相关实体,则必须仅依赖该区域的外键值。 您可以使用 ITable.GetModifiedMembers 来帮助仅检索已更改的值。

You can use GetChangeSet on the DataContext to retrieve a list of updates, inserts and deletes that have occurred on all tables within a context. You can use ITable.GetOriginalEntityState to retrieve the original values of a changed entity. However, when you retrieve the original values of a deleted or updated record, the associations will not be available so you will have to rely on foreign key values only in that area if you need to process related entities. You can Use ITable.GetModifiedMembers to help retrieve only values that have changed.

梦里°也失望 2024-07-25 21:53:42

请原谅我的回答可能很愚蠢,但是如何使用触发器直接在 SQL Server 中进行审核(如果您使用的是 SQL Server 2005 或 2008 标准)或使用 SQL Server 2008 Enterprise 中的更改跟踪工具?

Forgive me for perhaps a stupid answer, but how about doing the audit directly in the SQL Server using triggers (if you are in SQL Server 2005 or 2008 standard) or using the change tracking facilities in SQL server 2008 Enterprise?

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