286

c# EntityFramework: Get list of changed entities

c# EntityFramework: Get list of changed entities

The best way is to use the ChangeTracker object of the DbContext

 
            var changes = _context.ChangeTracker.Entries()
            .Where(t => t.State != EntityState.Unchanged )
            .Select(t => new {
                Original = t.OriginalValues.PropertyNames.ToDictionary(pn => pn, pn => t.OriginalValues[pn]),
                Current = t.CurrentValues.PropertyNames.ToDictionary(pn => pn, pn => t.CurrentValues[pn]),
            });
            foreach (var c in changes) {
                System.Diagnostics.Trace.WriteLine("To change:" + c.GetType().ToString());
            }

Comments (0)


This thread has been closed from taking new comments.