{"record":{"id":"b532cae48b104a93","repo":"dotnet/aspnetcore","slug":"more-than-one-sibling-of-element-frame-elementna","errorCode":null,"errorMessage":"More than one sibling of element '{frame.ElementNameField}' has the same key value, '{key}'. Key values must be unique.","messagePattern":"More than one sibling of element '(.+?)' has the same key value, '(.+?)'\\. Key values must be unique\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs","lineNumber":383,"sourceCode":"                    result[key] = new KeyedItemInfo(existingEntry.OldIndex, newStartIndex);\n                }\n            }\n\n            newStartIndex = NextSiblingIndex(frame, newStartIndex);\n        }\n\n        return result;\n    }\n\n    private static void ThrowExceptionForDuplicateKey(object key, in RenderTreeFrame frame)\n    {\n        switch (frame.FrameTypeField)\n        {\n            case RenderTreeFrameType.Component:\n                throw new InvalidOperationException($\"More than one sibling of component '{frame.ComponentTypeField}' has the same key value, '{key}'. Key values must be unique.\");\n\n            case RenderTreeFrameType.Element:\n                throw new InvalidOperationException($\"More than one sibling of element '{frame.ElementNameField}' has the same key value, '{key}'. Key values must be unique.\");\n\n            default:\n                throw new InvalidOperationException($\"More than one sibling has the same key value, '{key}'. Key values must be unique.\");\n        }\n    }\n\n    private static object KeyValue(ref RenderTreeFrame frame)\n    {\n        switch (frame.FrameTypeField)\n        {\n            case RenderTreeFrameType.Element:\n                return frame.ElementKeyField;\n            case RenderTreeFrameType.Component:\n                return frame.ComponentKeyField;\n            default:\n                return null;\n        }\n    }","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs#L365-L401","documentation":"Blazor uses @key to track element instances across re-renders for efficient DOM diffing. When RenderTreeDiffBuilder detects two sibling HTML elements (e.g., <div>, <li>) with the same non-null key value, it throws InvalidOperationException. The element name and key value are included in the message to aid debugging. Keys must be unique among siblings of the same parent.","triggerScenarios":"Rendering a list of HTML elements where @key is set to a duplicated value. For example: foreach (var row in Rows) { <tr key=\"row.Status\"><td>...</td></tr> } where Status is not unique. Also triggered by hardcoded keys on repeated elements.","commonSituations":"Keying list items by a non-unique field; using a constant key on all items; data with duplicate identifiers; using array index as key when items can be reordered (indices collide conceptually, though technically indices are unique, reordering causes remapping issues—not this specific error).","solutions":["Bind @key to a unique identifier for each item (database primary key, GUID, or composite key).","Verify the keyed property has no duplicates in the data source.","Use a composite key string if no single field is unique: key=\"$\"{item.A}-{item.B}\".\"","Remove @key if stable identity is not required."],"exampleFix":"<!-- before -->\n@foreach (var item in items)\n{\n    <li key=\"item.Type\">@item.Name</li> <!-- Type repeats -->\n}\n\n<!-- after -->\n@foreach (var item in items)\n{\n    <li key=\"item.Id\">@item.Name</li>\n}","handlingStrategy":"validation","validationCode":"// Validate key uniqueness for element lists\nstatic void AssertUniqueElementKeys<T>(IEnumerable<T> items, Func<T, object> keySelector)\n{\n    var seen = new HashSet<object>();\n    foreach (var item in items)\n    {\n        var key = keySelector(item);\n        if (key != null && !seen.Add(key))\n            throw new InvalidOperationException($\"Duplicate element key: {key}\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bind @key to a truly unique property (database ID, not category or status).","Write tests that verify data integrity (no duplicate IDs) before rendering lists.","If data may contain duplicates, deduplicate or add a synthetic unique key.","Avoid using constant or hardcoded keys on list items."],"tags":["blazor","key","diffing","element","razor","uniqueness"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}