public virtual EntityChangeSet CreateEntityChangeSet(ICollection <EntityEntry> entityEntries)
        {
            var changeSet = new EntityChangeSet
            {
                UserId   = BysSession.GetUserId() ?? 0,
                UserName = BysSession.UserName,
            };

            foreach (var entry in entityEntries)
            {
                if (!ShouldSaveEntityHistory(entry))
                {
                    continue;
                }

                var entityChangeInfo = CreateEntityChangeInfo(entry);
                if (entityChangeInfo == null)
                {
                    continue;
                }

                changeSet.EntityChanges.Add(entityChangeInfo);
            }

            return(changeSet);
        }
        /// <summary>
        /// Updates change time, entity id and foreign keys after SaveChanges is called.
        /// </summary>
        public void UpdateChangeSet(EntityChangeSet changeSet)
        {
            foreach (var entityChangeInfo in changeSet.EntityChanges)
            {
                /* Update change time */

                entityChangeInfo.ChangeTime = GetChangeTime(entityChangeInfo);

                /* Update entity id */

                var entityEntry = entityChangeInfo.EntityEntry.As <EntityEntry>();
                entityChangeInfo.EntityId = GetEntityId(entityEntry.Entity);

                /* Update foreign keys */

                var foreignKeys = entityEntry.Metadata.GetForeignKeys();

                foreach (var foreignKey in foreignKeys)
                {
                    foreach (var property in foreignKey.Properties)
                    {
                        var propertyEntry  = entityEntry.Property(property.Name);
                        var propertyChange = entityChangeInfo.PropertyChanges.FirstOrDefault(pc => pc.PropertyName == property.Name);

                        if (propertyChange == null)
                        {
                            if (!(propertyEntry.OriginalValue?.Equals(propertyEntry.CurrentValue) ?? propertyEntry.CurrentValue == null))
                            {
                                // Add foreign key
                                entityChangeInfo.PropertyChanges.Add(
                                    new EntityPropertyChange
                                {
                                    NewValue             = propertyEntry.CurrentValue.SafeToString(),
                                    OriginalValue        = propertyEntry.OriginalValue.SafeToString(),
                                    PropertyName         = property.Name,
                                    PropertyTypeFullName = property.ClrType.FullName
                                });
                            }

                            continue;
                        }

                        if (propertyChange.OriginalValue == propertyChange.NewValue)
                        {
                            var newValue = propertyEntry.CurrentValue.SafeToString();
                            if (newValue == propertyChange.NewValue)
                            {
                                // No change
                                entityChangeInfo.PropertyChanges.Remove(propertyChange);
                            }
                            else
                            {
                                // Update foreign key
                                propertyChange.NewValue = newValue;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
 public static async Task SaveAsync(IUnitOfWork unitOfWork, EntityChangeSet entityChangeSet)
 {
     await unitOfWork.Context.SaveChangesAsync();
 }