public override string ToString()
        {
            GenericSerializer <FileAttachmentMetadata> serializer = new GenericSerializer <FileAttachmentMetadata>();

            return(serializer.Serialize(this));
        }
        public void UpdatePerItemAttachmentChangesByCheckingRelatedItemRecords(
            string workItemId,
            ChangeGroup attachmentChangeGroup,
            out List <FileAttachmentMetadata> additionalAttachmentToDelete)
        {
            if (string.IsNullOrEmpty(workItemId))
            {
                throw new ArgumentNullException("workItemId");
            }

            additionalAttachmentToDelete = new List <FileAttachmentMetadata>();

            var queryByItem = QueryByItem(workItemId);
            var perItemExistingAttachments =
                from attch in queryByItem
                where attch.RelationshipExistsOnServer
                select attch;

            if (attachmentChangeGroup.Actions == null ||
                attachmentChangeGroup.Actions.Count == 0)
            {
                GenericSerializer <FileAttachmentMetadata> serializer = new GenericSerializer <FileAttachmentMetadata>();
                foreach (var attch in perItemExistingAttachments)
                {
                    if (!attch.OtherProperty.HasValue)
                    {
                        continue;
                    }
                    for (int i = 0; i < attch.OtherProperty.Value; ++i)
                    {
                        try
                        {
                            additionalAttachmentToDelete.Add(serializer.Deserialize(attch.RelatedArtifactId));
                        }
                        catch (Exception e)
                        {
                            TraceManager.TraceVerbose(e.ToString());
                        }
                    }
                }

                foreach (var attch in perItemExistingAttachments)
                {
                    attch.OtherProperty = 0;
                    attch.RelationshipExistsOnServer = false;
                }

                m_context.TrySaveChanges();
            }
            else
            {
                Dictionary <IMigrationFileAttachment, List <IMigrationAction> > perAttachmentChangeActions =
                    GroupAttachmentByMetadata(attachmentChangeGroup);

                List <IMigrationAction> skippedActions = new List <IMigrationAction>(attachmentChangeGroup.Actions.Count);
                foreach (var attchSpecificActions in perAttachmentChangeActions)
                {
                    if (attchSpecificActions.Value.Count == 0)
                    {
                        continue;
                    }

                    string attachmentStorageId    = attchSpecificActions.Key.ToString();
                    int    deltaAttachmentCount   = GetAttachmentCountInDelta(attchSpecificActions.Value);
                    var    attachmentInStore      = QueryItemSpecificAttachment(perItemExistingAttachments, attachmentStorageId);
                    int    attachmentInStoreCount = GetAttachmentInStoreCount(attachmentInStore);

                    int serverStoreCountDiff = deltaAttachmentCount - attachmentInStoreCount;
                    if (serverStoreCountDiff >= 0)
                    {
                        int redundantAttachmentActionCount = deltaAttachmentCount - serverStoreCountDiff;
                        var addAttachmentActionToSkip      =
                            attchSpecificActions.Value.Where(a => a.Action == WellKnownChangeActionId.AddAttachment).Take(redundantAttachmentActionCount);
                        skippedActions.AddRange(addAttachmentActionToSkip);
                    }
                    else if (serverStoreCountDiff < 0)
                    {
                        IMigrationAction action = attchSpecificActions.Value[0];
                        do
                        {
                            attachmentChangeGroup.Actions.Add(CreateDeleteAttachmentAction(action));
                        }while (++serverStoreCountDiff < 0);
                    }

                    int countAfterUpdateStore = UpdateStore(workItemId, attachmentStorageId, serverStoreCountDiff);
                    foreach (IMigrationAction action in attchSpecificActions.Value)
                    {
                        XmlElement attachmentNode =
                            action.MigrationActionDescription.DocumentElement.SelectSingleNode("/WorkItemChanges/Attachment") as XmlElement;
                        System.Diagnostics.Debug.Assert(null != attachmentNode, "attachmentNode is NULL");
                        attachmentNode.SetAttribute("CountInSourceSideStore", countAfterUpdateStore.ToString());
                    }
                }

                foreach (IMigrationAction skippedAction in skippedActions)
                {
                    attachmentChangeGroup.Actions.Remove(skippedAction);
                }
            }
        }