Exemple #1
0
		private ChangeEntity GetChangeEntity(DbEntityEntry entry, EntityState entityState)
		{
			if (!((BaseEntity)entry.Entity).IsTrackChanges)
				return null;

			List<ChangeField> changeFields = new List<ChangeField>();
			List<string> modifiedProperties;
			if (entityState == EntityState.Added || entityState == EntityState.Modified)
				modifiedProperties = entry.CurrentValues.PropertyNames.ToList();
			else if (entityState == EntityState.Deleted)
				modifiedProperties = entry.OriginalValues.PropertyNames.ToList();
			else
				throw new NotSupportedException(entityState.ToString());

			foreach (var modifiedPropertyName in modifiedProperties)
			{
				string newValue = null;
				string oldValue = null;
				if (entityState != EntityState.Deleted)
					newValue = String.Format("{0}", entry.CurrentValues[modifiedPropertyName]);

				if (entityState != EntityState.Added)
					oldValue = String.Format("{0}", entry.OriginalValues[modifiedPropertyName]);

				if (newValue != oldValue)
				{
					ChangeField changeField = new ChangeField();
					changeField.NewValue = newValue;
					changeField.OldValue = oldValue;
					changeField.FieldName = modifiedPropertyName;
					changeFields.Add(changeField);
				}
			}

			if (changeFields.Count != 0)
			{
				var changeEntity = new ChangeEntity()
				{
					EntityName = GetEntityName(entry.Entity),
					ChangeActionId = CreateChangeEntity(entityState),
					KeyValue = ((BaseEntity)entry.Entity).Id,
					ChangeFields = new List<ChangeField>()
				};

				foreach (var changeField in changeFields)
					changeEntity.ChangeFields.Add(changeField);
				return changeEntity;
			}
			else
			{
				return null;
			}
		}
Exemple #2
0
 private static IEnumerable<Tuple<string, string, object, string, object, object>> GetSavedValues(ObjectContext context, EntityState state
     , Func<ObjectStateEntry, IEnumerable<Tuple<string, object, object>>> getValues)
 {
     return context
         .ObjectStateManager
         .GetObjectStateEntries(state)
         .SelectMany(
             e =>
             {
                 string entityname = e.EntitySet.Name;
                 object id = null;
                 if (e.EntityKey != null && e.EntityKey.EntityKeyValues != null)
                     id = e.EntityKey.EntityKeyValues.Select(k => k.Value).FirstOrDefault();
                 return getValues(e).Select(t => Tuple.Create(state.ToString(), entityname, id, t.Item1, t.Item2, t.Item3));
             }
        );
 }
Exemple #3
0
		private string CreateChangeEntity(EntityState entityState)
		{
			switch (entityState)
			{
				case EntityState.Added:
					return ChangeAction.Added.ToString("g");
				case EntityState.Deleted:
					return ChangeAction.Deleted.ToString("g");
				case EntityState.Modified:
					return ChangeAction.Modified.ToString("g");
				default:
					throw new NotSupportedException(entityState.ToString());
			}
		}
        /// <summary>
        /// States the entry2 operation log.
        /// </summary>
        /// <param name="entitySet">The entity set.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="now">The now.</param>
        /// <param name="keyValue">The key value.</param>
        /// <param name="state">The state.</param>
        /// <returns>
        /// OperationLog object
        /// </returns>
        private OperationLog StateEntry2OperationLog(string entitySet, string objectType, DateTime now, string keyValue, EntityState state)
        {
            var userName = Thread.CurrentPrincipal.Identity.Name;

            if (String.IsNullOrEmpty(userName))
            {
                userName = "******";
            }

            var retVal = new OperationLog
            {
                LastModified = now,
                ObjectId = keyValue,
                ObjectType = objectType,
                TableName = entitySet,
                ModifiedBy = userName,
                OperationType = state.ToString()
            };

            return retVal;
        }