Beispiel #1
0
        public override Task <int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
        {
            foreach (var entry in ChangeTracker.Entries <AuditableEntity>())
            {
                switch (entry.State)
                {
                case EntityState.Added:
                    entry.Entity.CreatedAt         = DateTime.UtcNow;
                    entry.Entity.CreatedByMemberId = _currentUserService.GetMemberId();
                    break;

                case EntityState.Modified:
                    entry.Entity.LastModifiedAt         = DateTime.UtcNow;
                    entry.Entity.LastModifiedByMemberId = _currentUserService.GetMemberId();
                    break;

                case EntityState.Deleted:
                    if (entry.Entity is ISoftDeletable deletable)
                    {
                        // Unchanged so only the relevant columns are sent to the db
                        entry.State = EntityState.Unchanged;

                        deletable.DeletedAt         = DateTime.UtcNow;
                        deletable.IsDeleted         = true;
                        deletable.DeletedByMemberId = _currentUserService.GetMemberId();
                    }
                    break;
                }
            }

            return(base.SaveChangesAsync(cancellationToken));
        }