Example #1
0
        /// <summary>
        /// Will compare current entity with original one, and set modified properties as Modified
        /// and will consider InsertOnly and UpdateOnly properties also.
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <param name="currentEntry">Entry of the current entity</param>
        /// <param name="matchedEntityInDatabase">Original values of this entity in the database which will be compared with current entity</param>
        internal static void CompareWith <TEntity>(
            this DbEntityEntry <TEntity> currentEntry,
            TEntity matchedEntityInDatabase)
            where TEntity : class
        {
            // But firstly, get collection of properties which will never be updated.
            var notModifiableProperties = ExtendedEntityTypeConfiguration
                                          .GetInstance()
                                          .NotCompareProperties
                                          .OfReflectingType <TEntity>()
                                          .Select(prop => prop.Name);

            // Collection of properties which must be compared.
            var comparableProperties = currentEntry.CurrentValues
                                       .PropertyNames
                                       .Except(notModifiableProperties);

            foreach (var propertyName in comparableProperties)
            {
                var original = matchedEntityInDatabase.GetValue(propertyName);
                var current  = currentEntry.Entity.GetValue(propertyName);

                if (!Compare.IsEqual(original, current))
                {
                    currentEntry.Property(propertyName).IsModified = true;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Create properties collection which will be need to define model matches
        /// </summary>
        /// <typeparam name="TSource">Entity type</typeparam>
        /// <param name="context">Context instance</param>
        /// <param name="model">Entity instance</param>
        /// <param name="properties">Information about finded properties will be sent to each function recursively</param>
        /// <returns>ExtendedPropertyInfo collection which holds property information and their values</returns>
        internal static IEnumerable <ExtendedPropertyInfo> GetPropertiesForFiltering <TSource>(
            this DbContext context,
            TSource model,
            IEnumerable <ExtendedPropertyInfo> properties)
            where TSource : class
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Get primary key properties of the type alongside with the value
            var primaryKeyProperties = context.GetPrimaryKeys <TSource>()
                                       .MakePropertiesExtended(model);

            // Get unique properterties of the type alongside with the value
            var uniqueKeyProperties = ExtendedEntityTypeConfiguration
                                      .GetInstance()
                                      .UniqueKeys
                                      .OfReflectingType <TSource>()
                                      .MakePropertiesExtended(model);

            // If all primary key properties has non-default value
            if (primaryKeyProperties.Any() && !primaryKeyProperties.Select(prop => prop.PropertyValue).AnyDefault())
            {
                properties = properties.Concat(primaryKeyProperties);
            }
            else if (uniqueKeyProperties.Any())
            {
                properties = properties
                             .Concat(uniqueKeyProperties.Where(prop => prop.PropertyInfo.PropertyType.IsBuiltInType()));

                // Find complex property
                var complexProperty = uniqueKeyProperties
                                      .Where(prop => !prop.PropertyInfo.PropertyType.IsBuiltInType())
                                      .FirstOrDefault(prop => !prop.PropertyValue.IsDefault());

                // Recursively get its properties
                if (complexProperty != null)
                {
                    properties = properties.Add(complexProperty)
                                 .ToList();

                    properties = DbContextExtensions.GetPropertiesForFiltering(
                        context,
                        complexProperty.PropertyValue as dynamic,
                        properties);
                }
            }

            return(properties);
        }