public override Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { foreach (var entity in ChangeTracker.Entries() .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified) .Where(e => e.Entity is ISluggedEntity) .Select(e => e.Entity as ISluggedEntity) .Where(e => string.IsNullOrEmpty(e.Slug))) { entity.Slug = entity.GenerateSlug(this); } //remove all caches referencing this item foreach (ICachedEntity entity in ChangeTracker.Entries() .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified) .Where(e => e.Entity is ICachedEntity) .Select(e => e as ICachedEntity)) { foreach (CacheType type in Enum.GetValues(typeof(CacheType))) { try { if (entity != null) { //entity could not be cast as above var key = entity.GetCacheKey(type); if (!string.IsNullOrEmpty(key)) { _cache.InvalidateCacheResponseAsync( entity.GetCacheKey(type) ); } } } catch (Exception) { //hasn't been cached } } } return(base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken)); }