dotnet/efcore · error · ArgumentException
The number of elements ({elementCount}) must match the numbe
Error message
The number of elements ({elementCount}) must match the number of collection-index entries ({collectionIndicesCount}) when creating a RelationalJsonIndex. What it means
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.
Source
Thrown at src/EFCore.Relational/Metadata/RelationalJsonIndex.cs:43
public sealed class RelationalJsonIndex : IEquatable<RelationalJsonIndex>
{
/// <summary>
/// Creates a new <see cref="RelationalJsonIndex" /> instance.
/// </summary>
/// <param name="elements">The JSON elements targeted by the index, one per indexed property.</param>
/// <param name="collectionIndices">
/// The complex-collection indices traversed to reach each indexed property, parallel to
/// <paramref name="elements" />.
/// </param>
public RelationalJsonIndex(
IReadOnlyList<IRelationalJsonElement> elements,
IReadOnlyList<IReadOnlyList<int?>?>? collectionIndices)
{
Check.NotNull(elements);
if (collectionIndices is not null && elements.Count != collectionIndices.Count)
{
throw new ArgumentException(
RelationalStrings.JsonPathIndexElementsCollectionIndicesMismatch(elements.Count, collectionIndices.Count),
nameof(collectionIndices));
}
Elements = elements;
CollectionIndices = collectionIndices;
}
/// <summary>
/// Gets the JSON elements targeted by the index, one per indexed property.
/// </summary>
public IReadOnlyList<IRelationalJsonElement> Elements { get; }
/// <summary>
/// Gets the complex-collection indices traversed to reach each indexed property.
/// </summary>
public IReadOnlyList<IReadOnlyList<int?>?>? CollectionIndices { get; }
View on GitHub (pinned to dbf9771522)
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.
Example fix
// before new RelationalJsonIndex(elements, collectionIndices); // lengths differ // after Debug.Assert(collectionIndices == null || collectionIndices.Count == elements.Count); new RelationalJsonIndex(elements, collectionIndices);
Defensive patterns
Strategy: validation
Validate before calling
// Before constructing RelationalJsonIndex (provider/metadata code):
if (collectionIndices is not null && collectionIndices.Count != elements.Count)
throw new ArgumentException($"collectionIndices ({collectionIndices.Count}) must be parallel to elements ({elements.Count}).");
var index = new RelationalJsonIndex(elements, collectionIndices); Type guard
bool IsParallel(IReadOnlyList<IRelationalJsonElement> elements, IReadOnlyList<IReadOnlyList<int?>?>? ci)
=> ci is null || ci.Count == elements.Count; Prevention
- Treat `elements` and `collectionIndices` as parallel arrays; assert equal length in tests.
- Pass `null` for collectionIndices when no complex collection is traversed.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Model building is not supported when publishing with NativeA
- {numSortOrderProperties} values were provided in CreateIndex
- Invalid token type: '{tokenType}'.
- Relational-specific methods can only be used when the contex
- Timeout must be greater than or equal to zero. Provided time
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/2f085c617a8fb63d.
Report an issue: GitHub.