Search code examples
c#.net.net-coreentity-framework-core

Can't get changed entities after SaveChanges


I need to do some loggings after data successfully changed. I am using entity framework core for that purpose. There is SavingChanges event handler that is called BEFORE saving changes to DB and there is SavedChanges event handler that is called AFTER saving changes. In SavingChanges I can get modified entities by using context.ChangeTracker.Entries() and select entities with changed state context.ChangeTracker.Entries().Where(e => e.State != EntityState.Unchanged), but in SavedChanges event all entries are showing unchanged even though I have 3 changed entities. Can someone suggest good solution to my problem ?


Solution

  • Once they’ve been saved, they’re now unchanged as they represent the current state of the DB for those entities. Record which entities were modified/added/deleted in SavingChanges then use this information in SavedChanges to do whatever you need done with them.