public static List <EntityChangeLog> GetAdded(IEnumerable <EntityEntry> entities)
        {
            var addedEntities = entities
                                .Where(p => p.State == EntityState.Added).ToList();

            List <EntityChangeLog> listLog = null;

            if (addedEntities.Count > 0)
            {
                listLog = new List <EntityChangeLog>();
                foreach (var add in addedEntities)
                {
                    var entityName = add.Entity.GetType().Name;
                    var properties = add.OriginalValues.Properties;
                    var primaryKey = properties.FirstOrDefault(x => x.IsKey());
                    var log        = new ChangeLog {
                        Id = Guid.NewGuid()
                    };
                    log.PropertyCommon = new PropertyCommon
                    {
                        PrimaryKeyValue  = add.OriginalValues[primaryKey.Name].ToString(),
                        ActionType       = EntityState.Added,
                        DatetimeModified = DateTime.Now,
                        UserModified     = add.CurrentValues["UserCreated"]?.ToString()
                    };
                    log.NewObject = add.Entity;
                    var objectLog = new EntityChangeLog {
                        EntityName = entityName, ChangeLog = log
                    };
                    listLog.Add(objectLog);
                }
            }
            return(listLog);
        }
Пример #2
0
        private static EntityChangeLog ApplyAuditLog(EntityEntry entry, List <string> auditProperties,
                                                     Guid userId, DateTime dateTime)
        {
            var log = new EntityChangeLog(
                Guid.NewGuid(),
                entry.Entity.GetType().Name,
                ((EntityId)entry.Property(ModelBuilderExtensions.EntityId).CurrentValue).Id,
                entry.State.ToString(),
                dateTime,
                userId);

            foreach (var item in entry.Properties.Where(m => auditProperties.All(p => p != m.Metadata.Name)))
            {
                if (entry.State == EntityState.Added || item.IsModified)
                {
                    log.AddPropertyChangeLog(item.Metadata.Name, item.CurrentValue?.ToString());
                }
            }
            return(log);
            //Parallel.ForEach(entry.Properties.Where(m => auditProperties.All(p => p != m.Metadata.Name)),
            //    item =>
            //    {
            //        if (entry.State == EntityState.Added || item.IsModified)
            //        {
            //            var logDetail = new PropertyChangeLog
            //            {
            //                Id = Guid.NewGuid(),
            //                Key = item.Metadata.Name,
            //                Value = item.CurrentValue?.ToString()
            //            };
            //            log.PropertyChangeLogs.Add(logDetail);
            //        }
            //    });
        }
        public static List <EntityChangeLog> GetChangModifield(IEnumerable <EntityEntry> entities)
        {
            var modifiedEntities = entities
                                   .Where(p => p.State == EntityState.Modified).ToList();

            List <EntityChangeLog> listLog = null;

            if (modifiedEntities.Count > 0)
            {
                listLog = new List <EntityChangeLog>();
                foreach (var change in modifiedEntities)
                {
                    var entityName = change.Entity.GetType().Name;
                    var properties = change.OriginalValues.Properties;
                    if (properties.Count < 1)
                    {
                        break;
                    }
                    else
                    {
                        var primaryKey = properties.FirstOrDefault(x => x.IsKey());
                        var log        = new ChangeLog {
                            Id = Guid.NewGuid()
                        };
                        log.PropertyCommon = new PropertyCommon {
                            PrimaryKeyValue  = change.OriginalValues[primaryKey.Name].ToString(),
                            ActionType       = EntityState.Modified,
                            DatetimeModified = DateTime.Now,
                            UserModified     = change.CurrentValues["UserModified"]?.ToString()
                        };
                        log.NewObject = change.Entity;
                        //log.PropertyChange = new List<PropertyChange>();
                        foreach (var prop in properties)
                        {
                            if (prop.Name == "UserModified" || prop.Name == "DatetimeModified" ||
                                prop.Name == "DatetimeCreated" || prop.Name == "UserCreated")
                            {
                                continue;
                            }
                            var originalValue = change.OriginalValues[prop] != null? change.OriginalValues[prop].ToString(): string.Empty;
                            var currentValue  = change.CurrentValues[prop] != null? change.CurrentValues[prop].ToString(): string.Empty;
                            //if (prop.Name == "InactiveOn" && change.OriginalValues["Inactive"].ToString() == change.OriginalValues["Inactive"].ToString()) continue;
                            if (originalValue != currentValue)
                            {
                                var addObject = new PropertyChange()
                                {
                                    PropertyName = prop.Name,
                                    OldValue     = originalValue,
                                    NewValue     = currentValue
                                };
                                log.PropertyCommon.PropertyChange = addObject;
                            }
                        }
                        var objectLog = new EntityChangeLog {
                            EntityName = entityName, ChangeLog = log
                        };
                        listLog.Add(objectLog);
                    }
                }
            }
            return(listLog);
        }