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();
            }
        }