コード例 #1
0
        public T UpdateGraph <T>(T entity, IPropertyUpdater <T> projection) where T : class, IEntity
        {
            var allTrackedEntries = new List <EntityEntry>(context.ChangeTracker.Entries());

            var allProperties         = context.Entry(entity).Metadata.GetProperties().Select(p => p.Name);
            var propertiesToBeUpdated = new List <string>();

            foreach (var p in projection.AllProjections.BaseEntityProjections)
            {
                var projectionLambda = p as LambdaExpression;

                MemberExpression member = ParameterHelper.GetMemberExpression(projectionLambda);

                var propertyInfo = (PropertyInfo)member.Member;
                var propertyName = propertyInfo.Name;
                var propertyType = propertyInfo.PropertyType;

                context.Entry(entity).Property(propertyName).IsModified = true;

                propertiesToBeUpdated.Add(propertyName);
            }

            var propertiesToBeResetSinveTheyAreNotRequestedToBeUpdated = allProperties.Except(propertiesToBeUpdated);

            foreach (var propName in propertiesToBeResetSinveTheyAreNotRequestedToBeUpdated)
            {
                context.Entry(entity).Property(propName).CurrentValue = context.Entry(entity).Property(propName).OriginalValue;
            }

            foreach (var p in projection.AllProjections.NavigationPropertiesProjections)
            {
                var subentity = context.ChangeTracker
                                .Entries()
                                .SingleOrDefault(e => e.Metadata.ClrType.Name == p.Name && p.Id == (int)e.Property("Id").CurrentValue);
                if (subentity == null)
                {
                    continue;
                }

                var ahag = allTrackedEntries.Find(e => e.Metadata.ClrType.Name == p.Name && p.Id == (int)e.Property("Id").CurrentValue);

                allTrackedEntries.Remove(ahag);

                var allPropertieOfNavigationPropertys = subentity.Metadata.GetProperties().Select(p1 => p1.Name);
                var keyProperties = subentity.Metadata.GetKeys().SelectMany(p2 => p2.Properties).Select(p3 => p3.Name);
                var navigationPropertiesToBeUpdated = new List <string>();
                foreach (var pp in p.Projections)
                {
                    var projectionLambda = pp as LambdaExpression;

                    MemberExpression member = ParameterHelper.GetMemberExpression(projectionLambda);

                    var propertyInfo = (PropertyInfo)member.Member;
                    var propertyName = propertyInfo.Name;
                    var propertyType = propertyInfo.PropertyType;

                    if (keyProperties.Contains(propertyName))
                    {
                        continue;
                    }
                    subentity.Property(propertyName).IsModified = true;
                    navigationPropertiesToBeUpdated.Add(propertyName);
                }

                var navigationPropertiesToBeResetSinveTheyAreNotRequestedToBeUpdated = allPropertieOfNavigationPropertys.Except(navigationPropertiesToBeUpdated);
                foreach (var propName in navigationPropertiesToBeResetSinveTheyAreNotRequestedToBeUpdated)
                {
                    subentity.Property(propName).CurrentValue = subentity.Property(propName).OriginalValue;
                }
            }

            var ahag11 = allTrackedEntries.Find(e => e.Metadata.ClrType.Name == context.Entry(entity).Metadata.ClrType.Name&& entity.Id == (int)e.Property("Id").CurrentValue);

            allTrackedEntries.Remove(ahag11);

            foreach (var e in allTrackedEntries)
            {
                var allPropertieOfNavigationPropertys = e.Metadata.GetProperties().Select(p1 => p1.Name);
                foreach (var eey in allPropertieOfNavigationPropertys)
                {
                    e.Property(eey).CurrentValue = e.Property(eey).OriginalValue;
                }

                e.State = EntityState.Unchanged;
            }

            return(entity);
        }