private static Dictionary <IMigrationFileAttachment, List <IMigrationAction> > GroupAttachmentByMetadata(ChangeGroup attachmentChangeGroup)
        {
            var metadataComparer = new BasicFileAttachmentComparer();
            Dictionary <IMigrationFileAttachment, List <IMigrationAction> > perAttachmentChangeActions =
                new Dictionary <IMigrationFileAttachment, List <IMigrationAction> >(new BasicFileAttachmentComparer());

            foreach (IMigrationAction migrationAction in attachmentChangeGroup.Actions)
            {
                if (IsAttachmentSpecificChangeAction(migrationAction.Action))
                {
                    FileAttachmentMetadata attchMetadata = FileAttachmentMetadata.Create(migrationAction.MigrationActionDescription);
                    if (null != attchMetadata)
                    {
                        IMigrationFileAttachment existingKey = null;
                        foreach (IMigrationFileAttachment key in perAttachmentChangeActions.Keys)
                        {
                            if (metadataComparer.Equals(key, attchMetadata))
                            {
                                existingKey = key;
                                break;
                            }
                        }
                        if (null == existingKey)
                        {
                            existingKey = attchMetadata;
                            perAttachmentChangeActions.Add(existingKey, new List <IMigrationAction>());
                        }
                        perAttachmentChangeActions[existingKey].Add(migrationAction);
                    }
                }
            }

            return(perAttachmentChangeActions);
        }
        public void Update(string workItemId, IMigrationAction action)
        {
            FileAttachmentMetadata attchMetadata = FileAttachmentMetadata.Create(action.MigrationActionDescription);

            if (null != attchMetadata)
            {
                var queryByItem = QueryByItem(workItemId);
                if (action.Action == WellKnownChangeActionId.AddAttachment)
                {
                    if (queryByItem.Count() > 0)
                    {
                        queryByItem.First().RelationshipExistsOnServer = true;
                        if (queryByItem.First().OtherProperty.HasValue)
                        {
                            queryByItem.First().OtherProperty = queryByItem.First().OtherProperty.Value + 1;
                        }
                        else
                        {
                            queryByItem.First().OtherProperty = 1;
                        }
                    }
                    else
                    {
                        var newAttchRecord = CreateNewAttachmentStoreRecord(
                            workItemId, FileAttachmentMetadata.CreateAttachmentStorageId(action.MigrationActionDescription), 1);
                    }
                }
                else if (action.Action == WellKnownChangeActionId.DelAttachment)
                {
                    if (queryByItem.Count() > 0)
                    {
                        if (queryByItem.First().RelationshipExistsOnServer)
                        {
                            if (queryByItem.First().OtherProperty.HasValue&& queryByItem.First().OtherProperty.Value > 0)
                            {
                                queryByItem.First().OtherProperty = queryByItem.First().OtherProperty.Value - 1;
                                if (queryByItem.First().OtherProperty == 0)
                                {
                                    queryByItem.First().RelationshipExistsOnServer = false;
                                }
                            }
                            else
                            {
                                queryByItem.First().OtherProperty = 0;
                                queryByItem.First().RelationshipExistsOnServer = false;
                            }
                        }
                    }
                }

                m_context.TrySaveChanges();
            }
        }
        public int GetWorkItemAttachmentSpecificCount(string workItemId, XmlDocument updateDocument)
        {
            var queryByItem = QueryByItem(workItemId);
            var perItemExistingAttachments =
                from attch in queryByItem
                where attch.RelationshipExistsOnServer
                select attch;
            string attachmentStorageId = FileAttachmentMetadata.CreateAttachmentStorageId(updateDocument);
            var    attachmentInStore   = QueryItemSpecificAttachment(perItemExistingAttachments, attachmentStorageId);

            return(GetAttachmentInStoreCount(attachmentInStore));
        }
        internal static string CreateAttachmentStorageId(System.Xml.XmlDocument updateDocument)
        {
            FileAttachmentMetadata metadata = Create(updateDocument);

            if (null == metadata)
            {
                return(string.Empty);
            }
            else
            {
                return(metadata.ToString());
            }
        }