dotnet/efcore · error · InvalidOperationException

Only one ordering using scoring function is allowed. Use 'EF

Error message

Only one ordering using scoring function is allowed. Use 'EF.Functions.{rrf}' method to combine multiple scoring functions.

What it means

SelectExpression.AppendOrdering throws InvalidOperationException when you attempt to add a second scoring-function ordering. Only one scoring ordering is permitted per query; multiple scoring functions must be fused via EF.Functions.Rrf (Reciprocal Rank Fusion), which produces a single combined scoring expression.

Source

Thrown at src/EFCore.Cosmos/Query/Internal/Expressions/SelectExpression.cs:484

        _orderings.Clear();
        _orderings.Add(orderingExpression);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public void AppendOrdering(OrderingExpression orderingExpression)
    {
        if (_orderings.Count > 0)
        {
            var existingScoringFunctionOrdering = _orderings is [{ Expression: SqlFunctionExpression { IsScoringFunction: true } }];
            var appendingScoringFunctionOrdering = orderingExpression.Expression is SqlFunctionExpression { IsScoringFunction: true };
            if (appendingScoringFunctionOrdering || existingScoringFunctionOrdering)
            {
                throw new InvalidOperationException(
                    appendingScoringFunctionOrdering && existingScoringFunctionOrdering
                        ? CosmosStrings.OrderByMultipleScoringFunctionWithoutRrf(nameof(CosmosDbFunctionsExtensions.Rrf))
                        : CosmosStrings.OrderByScoringFunctionMixedWithRegularOrderby);
            }
        }

        if (_orderings.FirstOrDefault(o => o.Expression.Equals(orderingExpression.Expression)) == null)
        {
            _orderings.Add(orderingExpression);
        }
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>

View on GitHub (pinned to dbf9771522)

Solutions

  1. Combine the scoring functions into one RRF call and order by that: `OrderBy(a => EF.Functions.Rrf(score1, score2, ...))`.
  2. If fusing is not desired, pick the single most relevant scoring function and drop the others.
  3. Apply secondary ordering on a non-scoring expression only if it does not mix with scoring (see related error 108).

Example fix

// before
var q = ctx.Articles
    .OrderBy(a => EF.Functions.FullTextScore(a.Title, "x"))
    .OrderBy(a => EF.Functions.FullTextScore(a.Body, "y"));

// after
var q = ctx.Articles
    .OrderBy(a => EF.Functions.Rrf(
        EF.Functions.FullTextScore(a.Title, "x"),
        EF.Functions.FullTextScore(a.Body, "y")));
Defensive patterns

Strategy: validation

Validate before calling

// Static analysis helper: count scoring orderings in the query before executing.
// Ensure at most one; fuse extras via EF.Functions.Rrf.

Try / catch

try { var q = query.ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Rrf"))
{ /* combine scoring functions with EF.Functions.Rrf into one OrderBy */ }

Prevention

When it happens

Trigger: Chaining two OrderBy/ThenBy calls each using a scoring function: `.OrderBy(a => EF.Functions.FullTextScore(a.F1, "x")).OrderBy(a => EF.Functions.FullTextScore(a.F2, "y"))`.

Common situations: Wanting to weight several full-text matches independently; assuming Cosmos accepts multiple ORDER BY scoring clauses.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/aae4ba30806e6047. Report an issue: GitHub.