Linq to SQL - Audit Trail (find old values in SubmitChanges)
-
25-04-2021 - |
Question
At the moment I'm looking for a way to find the old values of an entity in the [context].SubmitChanges().
As far as I can tell only the new values show up. Do I really need to do a query to the database to get the old values of the entity?
The solution by 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);
}
Solution
Looks like you're looking for the Table.GetModifiedMembers method.
As you want old values of an entity, this method is useful.
OTHER TIPS
Check the below link.
http://www.matthidinger.com/archive/2008/05/08/linq-to-sql-audit-trail.aspx
You have your answer at http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/4498dddb-6dc2-4676-ba0a-eeda1aa453b0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow