{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/RelationalJsonIndex.cs#L25-L61","documentation":"RelationalJsonIndex requires the collectionIndices list to be parallel to elements: one collection-index entry per indexed element. The constructor throws ArgumentException when collectionIndices is non-null but its count differs from elements.Count.","triggerScenarios":"Constructing RelationalJsonIndex with a collectionIndices array whose length does not equal the number of elements; usually happens in convention/JSON-path-building code that zips two sequences of different lengths.","commonSituations":"Custom JSON index conventions; building multi-property JSON indexes where one path traverses a collection and another does not, but the caller supplies mismatched parallel arrays; provider scaffolding bugs.","solutions":["Ensure collectionIndices has exactly one entry per element; pass null for elements with no collection traversal.","Validate elements.Count == collectionIndices?.Count before constructing.","When only some elements traverse collections, build collectionIndices entry-by-entry matching each element.","Treat RelationalJsonIndex as internal infrastructure; prefer the fluent JSON APIs that construct it correctly."],"exampleFix":"// before\nvar idx = new RelationalJsonIndex(elements, someShorterList); // throws\n\n// after\nvar collectionIndices = elements\n    .Select(e => TraverseesCollection(e) ? BuildIndices(e) : null)\n    .ToList();\nvar idx = new RelationalJsonIndex(elements, collectionIndices);","handlingStrategy":"validation","validationCode":"if (collectionIndices is null || collectionIndices.Count == elements.Count)\n{\n    new RelationalJsonIndex(elements, collectionIndices);\n}","typeGuard":"static bool AreCollectionIndicesParallel(\n    IReadOnlyList<IRelationalJsonElement> elements,\n    IReadOnlyList<IReadOnlyList<int?>?>? collectionIndices)\n    => collectionIndices is null || collectionIndices.Count == elements.Count;","tryCatchPattern":null,"preventionTips":["Always build collectionIndices in lockstep with elements.","Treat RelationalJsonIndex as internal; prefer fluent JSON APIs.","Unit-test custom JSON-path conventions with multi-element indexes."],"tags":["ef-core","json","index","validation","internal-api"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}