/// <summary>
        /// Last SaveChange operation result
        /// </summary>
        public override int SaveChanges()
        {
            try
            {
                var createdSourceInfo  = ChangeTracker.Entries().Where(e => e.State == EntityState.Added);
                var modifiedSourceInfo = ChangeTracker.Entries().Where(e => e.State == EntityState.Modified);
                foreach (var entry in createdSourceInfo)
                {
                    if (entry.Entity.GetType().GetInterfaces().Contains(typeof(IHaveCreationDate)))
                    {
                        var creationDate = DateTime.Now;
                        var userName     = entry.Property("CreatedBy").CurrentValue == null
                            ? "Calabonga"
                            : entry.Property("CreatedBy").CurrentValue;
                        entry.Property("CreatedAt").CurrentValue = creationDate;
                        entry.Property("UpdatedAt").CurrentValue = creationDate;
                        entry.Property("CreatedBy").CurrentValue = userName;

                        _lastOPerationResult.AddMessage($"ChangeTracker has new entities: {entry.Entity.GetType()}");
                    }
                }

                foreach (var entry in modifiedSourceInfo)
                {
                    if (entry.Entity.GetType().GetInterfaces().Contains(typeof(IHaveCreationDate)))
                    {
                        var userName = entry.Property("UpdatedBy").CurrentValue == null
                            ? "Calabonga"
                            : entry.Property("UpdatedBy").CurrentValue;
                        entry.Property("UpdatedAt").CurrentValue = DateTime.Now;
                        entry.Property("UpdatedBy").CurrentValue = userName;
                    }
                    _lastOPerationResult.AddMessage($"ChangeTracker has modified entities: {entry.Entity.GetType()}");
                }

                return(base.SaveChanges());
            }
            catch (DbUpdateException exception)
            {
                _lastOPerationResult.Exception = exception;
                return(0);
            }
        }
Exemple #2
0
        /// <summary>
        /// Last SaveChange operation result
        /// </summary>

        public override int SaveChanges()
        {
            try {
                _lastOPerationResult = new SaveChangesResult();
                var createdSourceInfo  = ChangeTracker.Entries().Where(e => e.State == EntityState.Added);
                var modifiedSourceInfo = ChangeTracker.Entries().Where(e => e.State == EntityState.Modified);

                foreach (var entry in createdSourceInfo)
                {
                    if (entry.Entity.GetType().GetInterfaces().Contains(typeof(IAuditable)))
                    {
                        var creationDate = DateTime.Now;
                        entry.Property("CreatedAt").CurrentValue = creationDate;
                        entry.Property("UpdatedAt").CurrentValue = creationDate;

                        string userName = Thread.CurrentPrincipal.Identity?.Name ?? "Anonymous";
                        //entry.Property("CreatedBy").CurrentValue = userName;
                        entry.Property("UpdatedBy").CurrentValue = userName;


                        _lastOPerationResult.AddMessage($"ChangeTracker has new entities: {entry.Entity.GetType()}");
                    }
                }
                foreach (var entry in modifiedSourceInfo)
                {
                    if (entry.Entity.GetType().GetInterfaces().Contains(typeof(IAuditable)))
                    {
                        entry.Property("UpdatedAt").CurrentValue = DateTime.Now;
                    }
                    _lastOPerationResult.AddMessage($"ChangeTracker has modified entities: {entry.Entity.GetType()}");
                }
                return(base.SaveChanges());
            }
            catch (DbUpdateException exception) {
                _lastOPerationResult.Exception = exception;
                return(0);
            }
        }