예제 #1
0
        public EntityDataLog SaveAndLog(T entity, params object[] id)
        {
            EntityDataLog entityDataLog = this.Logs(entity);

            this.Save(entity, id);
            return(entityDataLog);
        }
예제 #2
0
        public virtual EntityDataLog DeleteAndLog(params object[] id)
        {
            EntityDataLog entityDataLog = (EntityDataLog)null;
            T             entity        = this.dbset.Find(id);

            if ((object)entity != null)
            {
                entityDataLog = this.Logs(entity);
                this.RemoveEntity(entity);
            }
            return(entityDataLog);
        }
예제 #3
0
        public EntityDataLog Logs(T entity)
        {
            EntityDataLog entityDataLog = new EntityDataLog();
            Type          type          = typeof(T);

            entityDataLog.FullName = type.FullName;
            DisplayNameAttribute[] customAttributes1 = ReflectionExtensions.GetCustomAttributes <DisplayNameAttribute>(type);
            DisplayAttribute[]     customAttributes2 = ReflectionExtensions.GetCustomAttributes <DisplayAttribute>(type);
            if (customAttributes1 != null && customAttributes1.Length > 0)
            {
                entityDataLog.DisplayName = customAttributes1[0].DisplayName;
            }
            else if (customAttributes2 != null && customAttributes2.Length > 0)
            {
                entityDataLog.DisplayName = customAttributes2[0].Name;
            }
            // ISSUE: reference to a compiler-generated method
            DbEntityEntry <T> dbEntityEntry = this.DataContext.Entry <T>(entity);

            if (dbEntityEntry.State == EntityState.Modified)
            {
                foreach (string propertyName in dbEntityEntry.OriginalValues.PropertyNames)
                {
                    EntityPropertyData entityPropertyData = new EntityPropertyData();
                    entityPropertyData.Name = propertyName;
                    DisplayNameAttribute displayAttribute1 = ClassExtensions <T> .GetDisplayAttribute <DisplayNameAttribute>(propertyName);

                    DisplayAttribute displayAttribute2 = ClassExtensions <T> .GetDisplayAttribute(propertyName);

                    if (!string.IsNullOrEmpty(displayAttribute1.DisplayName))
                    {
                        entityPropertyData.DisplayName = displayAttribute1.DisplayName;
                    }
                    else if (!string.IsNullOrEmpty(displayAttribute2.Name))
                    {
                        entityPropertyData.DisplayName = displayAttribute2.Name;
                    }
                    entityPropertyData.OldValue = dbEntityEntry.OriginalValues[propertyName];
                    entityPropertyData.NewValue = dbEntityEntry.CurrentValues[propertyName];
                    entityDataLog.ChangedProperties.Add(entityPropertyData);
                }
            }
            else
            {
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    EntityPropertyData entityPropertyData = new EntityPropertyData();
                    entityPropertyData.Name = property.Name;
                    DisplayNameAttribute displayAttribute1 = ClassExtensions <T> .GetDisplayAttribute <DisplayNameAttribute>(entityPropertyData.Name);

                    DisplayAttribute displayAttribute2 = ClassExtensions <T> .GetDisplayAttribute(entityPropertyData.Name);

                    if (!string.IsNullOrEmpty(displayAttribute1.DisplayName))
                    {
                        entityPropertyData.DisplayName = displayAttribute1.DisplayName;
                    }
                    else if (!string.IsNullOrEmpty(displayAttribute2.Name))
                    {
                        entityPropertyData.DisplayName = displayAttribute2.Name;
                    }
                    entityPropertyData.OldValue = property.GetValue((object)entity, (object[])null);
                    entityDataLog.ChangedProperties.Add(entityPropertyData);
                }
            }
            return(entityDataLog);
        }