dotnet/efcore · error · InvalidOperationException

Ordering using a scoring function is mutually exclusive with

Error message

Ordering using a scoring function is mutually exclusive with other forms of ordering.

What it means

SelectExpression.AppendOrdering throws InvalidOperationException when a scoring-function ordering is mixed with a regular (non-scoring) ordering in the same query. The two ordering modes are mutually exclusive on Cosmos: you either order by one scoring function or by regular expressions, never both.

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. Choose one ordering mode: either a single scoring function (optionally fused via RRF) OR regular property ordering — not both.
  2. Move the secondary sort client-side after materializing the server results.
  3. If relevance must be primary and a property secondary, accept server relevance ordering and re-sort the top-N client-side.

Example fix

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

// after (scoring only; tie-break client-side)
var q = ctx.Articles
    .OrderBy(a => EF.Functions.FullTextScore(a.Body, "x"));
// then: results.OrderByDescending(r => r.Published) in memory
Defensive patterns

Strategy: validation

Validate before calling

// Decide ordering mode up front: scoring XOR regular, never both.
enum OrderMode { Scoring, Regular }
static IQueryable<T> ApplyOrdering<T>(IQueryable<T> q, OrderMode mode) => mode switch
{
    OrderMode.Scoring => q.OrderBy(/* scoring fn */),
    _ => q.OrderBy(/* property */),
};

Try / catch

try { var q = query.ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("mutually exclusive"))
{ /* drop one of the orderings or move tie-break client-side */ }

Prevention

When it happens

Trigger: Mixing modes, e.g. `.OrderBy(a => a.Published).OrderBy(a => EF.Functions.FullTextScore(...))` or the reverse — appending a regular ordering after a scoring one (or vice versa).

Common situations: Adding a deterministic tie-breaker property alongside a relevance score; porting a multi-key relational OrderBy to a full-text Cosmos query.

Related errors


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