Linq到SQL-审计跟踪(在SubmitChanges中查找旧值)

本文关键字:查找 SubmitChanges SQL- 审计跟踪 Linq | 更新日期: 2023-09-27 18:22:32

目前我正在寻找一种在[context].SubmitChanges().中查找实体旧值的方法

据我所知,只有新的价值观出现了。我真的需要对数据库进行查询以获取实体的旧值吗?

GertArnold的解决方案:

    public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
                {
                    // Get the changeset
                    ChangeSet changeSet = this.GetChangeSet();
                    // Put the updated objects into a IEnumerable
                    IEnumerable<object> updatedEntities = changeSet.Updates;
                    foreach (var entity in updatedEntities.Where(entity => AuditTypes.Contains(entity.GetType())))
                    {
                        var old = this.GetTable(entity.GetType()).GetModifiedMembers(entity);
                        // Do something with the old values
                    }
                    // Save the changes
                    base.SubmitChanges(failureMode);
                }

Linq到SQL-审计跟踪(在SubmitChanges中查找旧值)

看起来您正在寻找Table.GetModifiedMembers方法。

由于您希望的旧值为实体,因此此方法非常有用。

查看下面的链接。

http://www.matthidinger.com/archive/2008/05/08/linq-to-sql-audit-trail.aspx

你的答案在http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/4498dddb-6dc2-4676-ba0a-eeda1aa453b0