{"record":{"id":"994c7eefc2ba620d","repo":"stride3d/stride","slug":"the-guid-of-the-key-does-not-match-the-guid-of-the-value","errorCode":null,"errorMessage":"The guid of the key does not match the guid of the value","messagePattern":"The guid of the key does not match the guid of the value","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/AssetPartCollection.cs","lineNumber":22,"sourceCode":"using Stride.Core.Serialization;\n\nnamespace Stride.Core.Assets;\n\n[DataSerializer(typeof(AssetPartCollectionSerializer<,>), Mode = DataSerializerGenericMode.GenericArguments)]\npublic sealed class AssetPartCollection<TAssetPartDesign, TAssetPart> : SortedList<Guid, TAssetPartDesign>\n    where TAssetPartDesign : IAssetPartDesign<TAssetPart>\n    where TAssetPart : IIdentifiable\n{\n    public void Add(TAssetPartDesign part)\n    {\n        if (part == null) throw new ArgumentNullException(nameof(part));\n        Add(part.Part.Id, part);\n    }\n\n    public void Add(KeyValuePair<Guid, TAssetPartDesign> part)\n    {\n        if (part.Value == null) throw new ArgumentNullException(nameof(part));\n        if (part.Key != part.Value.Part.Id) throw new ArgumentException(@\"The guid of the key does not match the guid of the value\", nameof(part));\n        Add(part.Key, part.Value);\n    }\n\n    /// <summary>\n    /// Refreshes the keys of this collection. Must be called if some ids of the contained parts have changed.\n    /// </summary>\n    public void RefreshKeys()\n    {\n        var values = Values.ToList();\n        Clear();\n        foreach (var value in values)\n        {\n            Add(value.Part.Id, value);\n        }\n    }\n}\n\npublic class AssetPartCollectionSerializer<TAssetPartDesign, TAssetPart> : DataSerializer<AssetPartCollection<TAssetPartDesign, TAssetPart>>, IDataSerializerGenericInstantiation","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/AssetPartCollection.cs#L4-L40","documentation":"AssetPartCollection.Add(KeyValuePair<Guid,TAssetPartDesign>) throws ArgumentException when the dictionary key does not equal part.Value.Part.Id. The collection (a SortedList keyed by Guid) enforces that the key always matches the id inside the design so lookups by part id stay consistent.","triggerScenarios":"Calling Add(new KeyValuePair<Guid,TAssetPartDesign>(someGuid, design)) where someGuid != design.Part.Id.","commonSituations":"Building the collection from serialized key/value pairs where the id of the part was changed after the key was computed (e.g. ids updated during a refresh but keys not refreshed).","solutions":["Use the key design.Part.Id when constructing the KeyValuePair.","Call Add(TAssetPartDesign) directly and let the collection key on Part.Id itself.","Call RefreshKeys() on the collection after changing ids of contained parts."],"exampleFix":"// before\ncollection.Add(new KeyValuePair<Guid, Design>(oldGuid, design));\n// after\ncollection.Add(new KeyValuePair<Guid, Design>(design.Part.Id, design));","handlingStrategy":"validation","validationCode":"if (kvp.Key != kvp.Value?.Part.Id) throw new ArgumentException(\"Key must equal kvp.Value.Part.Id\");","typeGuard":null,"tryCatchPattern":"try { collection.Add(kvp); } catch (ArgumentException ex) when (ex.Message.Contains(\"guid of the key\")) { collection.Add(kvp.Value.Part.Id, kvp.Value); }","preventionTips":["Prefer Add(TAssetPartDesign) which keys on Part.Id automatically.","Call RefreshKeys() after mutating any part's Id.","Never construct KeyValuePair entries by hand from stale ids."],"tags":["assets","guid","collection"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}