private static Entity MergeClone(OrganizationServiceContext context, Entity entity, bool includeRelatedEntities = false)
        {
            if (entity == null)
            {
                return(null);
            }

            // aggregate queries return blank entities

            if (entity.Id == Guid.Empty)
            {
                return(entity.Clone(includeRelatedEntities));
            }

            var attached = context.GetAttachedEntities().FirstOrDefault(e => e.Id == entity.Id);

            if (attached != null)
            {
                // use the existing entity instead
                return(attached);
            }

            return(context.AttachClone(entity, includeRelatedEntities));
        }