dotnet/efcore · error · ArgumentException
The number of elements
Error message
The number of elements ({elementCount}) must match the number of collection-index entries ({collectionIndicesCount}) when creating a RelationalJsonIndex. What it means
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.
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.
Example fix
// before
var idx = new RelationalJsonIndex(elements, someShorterList); // throws
// after
var collectionIndices = elements
.Select(e => TraverseesCollection(e) ? BuildIndices(e) : null)
.ToList();
var idx = new RelationalJsonIndex(elements, collectionIndices); Defensive patterns
Strategy: validation
Validate before calling
if (collectionIndices is null || collectionIndices.Count == elements.Count)
{
new RelationalJsonIndex(elements, collectionIndices);
} Type guard
static bool AreCollectionIndicesParallel(
IReadOnlyList<IRelationalJsonElement> elements,
IReadOnlyList<IReadOnlyList<int?>?>? collectionIndices)
=> collectionIndices is null || collectionIndices.Count == elements.Count; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Specified argument was out of the range of valid values…
- The indexes on ' ' and on ' ' are both mapped to ' ', but…
- The indexes on ' ' and on ' ' are both mapped to ' . ', but…
- The indexes on ' ' and on ' ' are both mapped to ' . ', but…
- The indexes on ' ' and on ' ' are both mapped to ' . ', but…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/2f085c617a8fb63d.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)