Example #1
0
        public override int SaveChanges(bool acceptAllChangesOnSuccess)
        {
            var entries = ChangeTracker.Entries();

            foreach (var entry in entries)
            {
                if (!(entry.Entity is IEntityBase entity))
                {
                    continue;
                }

                if (entry.State == EntityState.Added)
                {
                    entity.CreatedAt  = DateTimeService.NowOffset();
                    entity.CreatedBy  = -1;
                    entity.ModifiedAt = DateTimeService.NowOffset();
                    entity.ModifiedBy = -1;
                }
                else if (entry.State == EntityState.Modified)
                {
                    entry.CurrentValues[nameof(entity.CreatedAt)] = entry.OriginalValues[nameof(entity.CreatedAt)];
                    entry.CurrentValues[nameof(entity.CreatedBy)] = entry.OriginalValues[nameof(entity.CreatedBy)];

                    entity.ModifiedAt = DateTimeService.NowOffset();
                    entity.ModifiedBy = -1;
                }
            }

            return(base.SaveChanges(acceptAllChangesOnSuccess));
        }