{"record":{"id":"a281e7716b274ab6","repo":"bchavez/Bogus","slug":"an-item-with-the-same-key-has-already-been-added","errorCode":null,"errorMessage":"An item with the same key has already been added.","messagePattern":"An item with the same key has already been added\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Rule.cs","lineNumber":67,"sourceCode":"      values[key2] = value;\n   }\n}\n\npublic class MultiSetDictionary<Key, Value> : Dictionary<Key, HashSet<Value>>\n{\n   public MultiSetDictionary(IEqualityComparer<Key> comparer) : base(comparer)\n   {\n   }\n\n   public void Add(Key key, Value value)\n   {\n      if( !this.TryGetValue(key, out var values) )\n      {\n         values = new HashSet<Value>();\n         this.Add(key, values);\n      }\n      if( values.Contains(value) )\n         throw new ArgumentException(\"An item with the same key has already been added.\");\n      values.Add(value);\n   }\n}","sourceCodeStart":49,"sourceCodeEnd":70,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Rule.cs#L49-L70","documentation":"Thrown by MultiSetDictionary.Add when a value for a key is already present in that key's HashSet. MultiSetDictionary maps one key to many distinct values; attempting to add a duplicate (key,value) pair is rejected. This is internal infrastructure backing Bogus rule tracking.","triggerScenarios":"Internal: registering the same rule binding (member -> setter) twice within the same MultiSetDictionary instance. Users typically hit it indirectly via duplicate RuleFor registrations on the same member within a context that dedupes.","commonSituations":"Calling RuleFor on the same property twice in a way that feeds the multiset; custom extensions that push into internal rule storage.","solutions":["Avoid registering duplicate rules for the same member; later rules should replace, check the API you are using.","If extending Bogus internals, check values.Contains(value) before adding.","Review RuleFor/RuleForType call sites for accidental double-registration."],"exampleFix":"// before\nf.RuleFor(x => x.Id, _ => 1);\nf.RuleFor(x => x.Id, _ => 2); // if routed through multiset\n// after\nf.RuleFor(x => x.Id, _ => 2); // single registration, or use the override that replaces","handlingStrategy":"validation","validationCode":"// internal multiset: check before adding\nif (!multiset.TryGetValue(key, out var set) || !set.Contains(value))\n    multiset.Add(key, value);","typeGuard":null,"tryCatchPattern":"try { dict.Add(key, value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"same key has already been added\")) {\n    // skip duplicate or replace existing value\n}","preventionTips":["Avoid duplicate RuleFor registrations on the same member.","If extending internals, guard with Contains before Add.","Audit rule registration loops for repeated bindings."],"tags":["bogus","rule","multiset","duplicate-key"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}