{"record":{"id":"544771e4dabb49af","repo":"dotnet/wpf","slug":"sr-annotationalreadyexists","errorCode":null,"errorMessage":"SR.AnnotationAlreadyExists","messagePattern":"SR\\.AnnotationAlreadyExists","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs","lineNumber":134,"sourceCode":"        public override void AddAnnotation(Annotation newAnnotation)\n        {\n            ArgumentNullException.ThrowIfNull(newAnnotation);\n\n            // We are going to modify internal data. Lock the object\n            // to avoid modifications from other threads\n            lock (SyncRoot)\n            {\n                //fire trace event\n                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAnnotationBegin);\n                try\n                {\n                    CheckStatus();\n\n                    XPathNavigator editor = GetAnnotationNodeForId(newAnnotation.Id);\n\n                    // we are making sure that the newAnnotation doesn't already exist in the store\n                    if (editor != null)\n                        throw new ArgumentException(SR.AnnotationAlreadyExists, nameof(newAnnotation));\n\n                    // we are making sure that the newAnnotation doesn't already exist in the store map\n                    if (_storeAnnotationsMap.FindAnnotation(newAnnotation.Id) != null)\n                        throw new ArgumentException(SR.AnnotationAlreadyExists, nameof(newAnnotation));\n\n                    // simply add the annotation to the map to save on performance\n                    // notice that we need to tell the map that this instance of the annotation is dirty\n                    _storeAnnotationsMap.AddAnnotation(newAnnotation, true);\n                }\n                finally\n                {\n                    //fire trace event\n                    EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAnnotationEnd);\n                }\n\t\t\t}\n\n            OnStoreContentChanged(new StoreContentChangedEventArgs(StoreContentAction.Added, newAnnotation));\n        }","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs#L116-L152","documentation":"XmlStreamStore.AddAnnotation rejects annotations whose Id already exists in the store, throwing ArgumentException(SR.AnnotationAlreadyExists, nameof(newAnnotation)). Annotation Ids are unique keys within the store, so adding a duplicate would corrupt the store's invariants.","triggerScenarios":"Calling store.AddAnnotation(annotation) with an annotation whose Id matches one already persisted (or already present in the in-memory store map), e.g. re-adding a loaded annotation or cloning one without regenerating its Id.","commonSituations":"Re-running an import routine without clearing the store; copying annotation objects (clone/copy constructors preserve Id); deserializing the same annotation twice into one store.","solutions":["Check GetAnnotations() or the annotation's existence before adding, and skip or update instead of adding duplicates.","Generate a fresh unique Id (Guid) when re-adding a copied annotation.","For updates to an existing annotation, use the appropriate update/modify API rather than AddAnnotation."],"exampleFix":"// before\nstore.AddAnnotation(loadedAnnotation); // throws if Id already stored\n// after\nif (store.GetAnnotations(new Guid(loadedAnnotation.Id.ToString())).FirstOrDefault() == null)\n    store.AddAnnotation(loadedAnnotation);\nelse\n    loadedAnnotation = new Annotation(Guid.NewGuid(), loadedAnnotation.Cargos); // new Id for the copy","handlingStrategy":"validation","validationCode":"bool exists = store.GetAnnotations().Cast<Annotation>().Any(a => a.Id == newAnnotation.Id);\nif (!exists) store.AddAnnotation(newAnnotation);","typeGuard":null,"tryCatchPattern":"try { store.AddAnnotation(newAnnotation); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already exists\")) { /* treat as duplicate: update or skip */ }","preventionTips":["Regenerate Ids (new Guid) when cloning or copying annotations.","Make import routines idempotent by checking existence before adding.","Use one code path for add-vs-update instead of re-adding loaded annotations."],"tags":["wpf","annotations","duplicate","argument-validation"],"backgroundTag":"file-already-exists","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}