How to determine the changed entity on a ISaveChangesInterceptor SavedChangesAsync on dotnet core

Viewed 118

When implementing ISaveChangesInterceptor. I need is to get a handle to the entity that was written to the database. So I can confirm it was a success, as I also would like to have database created values to the record as inserted. for example here on code bellow it never is Added it is Unchanged. While if I override the SavingChangesAsync it is Modified. But, the SavingChangesAsync occurs before writing into the database and SavedChanges is after. So the SavingChanges do not have db computed values as I think occurs before any error that may be encounter while saving.

public async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEventData eventData, int result, CancellationToken cancellationToken = default)
{
         eventData.Context.ChangeTracker.DetectChanges();
         foreach (var entity in eventData.Context.ChangeTracker.Entries())
         {                
            if (entity.State == Microsoft.EntityFrameworkCore.EntityState.Added)
            {
            }
         }
}
0 Answers
Related