{"record":{"id":"2f085c617a8fb63d","repo":"dotnet/efcore","slug":"the-number-of-elements-elementcount-must-match","errorCode":null,"errorMessage":"The number of elements ({elementCount}) must match the number of collection-index entries ({collectionIndicesCount}) when creating a RelationalJsonIndex.","messagePattern":"The number of elements \\((.+?)\\) must match the number of collection-index entries \\((.+?)\\) when creating a RelationalJsonIndex\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/RelationalJsonIndex.cs","lineNumber":43,"sourceCode":"public sealed class RelationalJsonIndex : IEquatable<RelationalJsonIndex>\n{\n    /// <summary>\n    ///     Creates a new <see cref=\"RelationalJsonIndex\" /> instance.\n    /// </summary>\n    /// <param name=\"elements\">The JSON elements targeted by the index, one per indexed property.</param>\n    /// <param name=\"collectionIndices\">\n    ///     The complex-collection indices traversed to reach each indexed property, parallel to\n    ///     <paramref name=\"elements\" />.\n    /// </param>\n    public RelationalJsonIndex(\n        IReadOnlyList<IRelationalJsonElement> elements,\n        IReadOnlyList<IReadOnlyList<int?>?>? collectionIndices)\n    {\n        Check.NotNull(elements);\n\n        if (collectionIndices is not null && elements.Count != collectionIndices.Count)\n        {\n            throw new ArgumentException(\n                RelationalStrings.JsonPathIndexElementsCollectionIndicesMismatch(elements.Count, collectionIndices.Count),\n                nameof(collectionIndices));\n        }\n\n        Elements = elements;\n        CollectionIndices = collectionIndices;\n    }\n\n    /// <summary>\n    ///     Gets the JSON elements targeted by the index, one per indexed property.\n    /// </summary>\n    public IReadOnlyList<IRelationalJsonElement> Elements { get; }\n\n    /// <summary>\n    ///     Gets the complex-collection indices traversed to reach each indexed property.\n    /// </summary>\n    public IReadOnlyList<IReadOnlyList<int?>?>? CollectionIndices { get; }\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/RelationalJsonIndex.cs#L25-L61","documentation":"Thrown by the RelationalJsonIndex constructor when the `elements` list and the `collectionIndices` list have different lengths. These two arguments must be parallel (one collection-index path per indexed JSON element), so a mismatch indicates the caller built the index metadata incorrectly.","triggerScenarios":"Constructing `new RelationalJsonIndex(elements, collectionIndices)` where `collectionIndices.Count != elements.Count`. This is internal/provider-level metadata construction reached when a provider builds a relational index over a JSON-mapped column and supplies a mismatched number of collection-index paths.","commonSituations":"Writing a custom relational provider or extending JSON-index mapping; a bug in provider JSON-index codegen that emits collection indices for only some indexed properties; mismatched arrays after refactoring index columns.","solutions":["Ensure `collectionIndices` has exactly one entry per element in `elements` (or pass `null` when no complex collections are traversed).","If you are a provider author, audit the call site that builds `RelationalJsonIndex` and produce a parallel list.","Add a unit test asserting `elements.Count == collectionIndices.Count` before construction."],"exampleFix":"// before\nnew RelationalJsonIndex(elements, collectionIndices); // lengths differ\n// after\nDebug.Assert(collectionIndices == null || collectionIndices.Count == elements.Count);\nnew RelationalJsonIndex(elements, collectionIndices);","handlingStrategy":"validation","validationCode":"// Before constructing RelationalJsonIndex (provider/metadata code):\nif (collectionIndices is not null && collectionIndices.Count != elements.Count)\n    throw new ArgumentException($\"collectionIndices ({collectionIndices.Count}) must be parallel to elements ({elements.Count}).\");\nvar index = new RelationalJsonIndex(elements, collectionIndices);","typeGuard":"bool IsParallel(IReadOnlyList<IRelationalJsonElement> elements, IReadOnlyList<IReadOnlyList<int?>?>? ci)\n    => ci is null || ci.Count == elements.Count;","tryCatchPattern":null,"preventionTips":["Treat `elements` and `collectionIndices` as parallel arrays; assert equal length in tests.","Pass `null` for collectionIndices when no complex collection is traversed."],"tags":["efcore","json","index","metadata","argument","relational"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}