dotnet/efcore · error · InvalidOperationException

The ' ' value passed to ' ' must be a constant.

Error message

The '{parameter}' value passed to '{methodName}' must be a constant.

What it means

CosmosVectorSearchTranslator throws ArgumentNotConstant when the useBruteForce argument passed to EF.Functions.VectorDistance is not a SqlConstantExpression. Cosmos DB requires this argument to be a literal so it can be inlined into the generated SQL; non-constant values cannot be translated for this parameter.

Solutions

  1. Pass a literal bool (true/false) or null for useBruteForce.
  2. If the value comes from configuration, inline the resolved constant value directly in the expression.
  3. Build separate query branches for the brute-force and non-brute-force cases.

Example fix

// before
var brute = settings.UseBruteForce;
var q = db.Items.OrderBy(i => EF.Functions.VectorDistance(i.Vec, query, brute, null));

// after - inline the constant
var q = db.Items.OrderBy(i => EF.Functions.VectorDistance(i.Vec, query, true, null));
Defensive patterns

Strategy: validation

Validate before calling

// Only pass compile-time literals for useBruteForce.
const bool UseBruteForce = true; // resolved at compile time
var q = db.Items.OrderBy(i => EF.Functions.VectorDistance(i.Vec, query, UseBruteForce, null));

Prevention

When it happens

Trigger: Passing a variable, parameter, computed value, or column as the useBruteForce argument to VectorDistance(prop, vec, useBruteForce, options).

Common situations: Reading the brute-force flag from configuration and passing the variable; passing a method-call result; binding a parameter from a closure.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/738e7247b637bfbc. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Query/Internal/Translators/CosmosVectorSearchTranslator.cs:45

        SqlExpression? instance,
        MethodInfo method,
        IReadOnlyList<SqlExpression> arguments,
        IDiagnosticsLogger<DbLoggerCategory.Query> logger)
    {
        if (method.DeclaringType != typeof(CosmosDbFunctionsExtensions)
            || method.Name != nameof(CosmosDbFunctionsExtensions.VectorDistance))
        {
            return null;
        }

        if (arguments is not [_, var vector1, var vector2, var useBruteForceExpression, var optionsExpression])
        {
            throw new UnreachableException();
        }

        if (useBruteForceExpression is not SqlConstantExpression { Value: var useBruteForceValue })
        {
            throw new InvalidOperationException(
                CoreStrings.ArgumentNotConstant("useBruteForce", nameof(CosmosDbFunctionsExtensions.VectorDistance)));
        }

        if (optionsExpression is not SqlConstantExpression { Value: var optionsValue })
        {
            throw new InvalidOperationException(
                CoreStrings.ArgumentNotConstant("options", nameof(CosmosDbFunctionsExtensions.VectorDistance)));
        }

        var options = (VectorDistanceOptions?)optionsValue;

        var vectorMapping = vector1.TypeMapping as CosmosVectorTypeMapping
            ?? vector2.TypeMapping as CosmosVectorTypeMapping
            ?? throw new InvalidOperationException(CosmosStrings.VectorSearchRequiresVector);

        var vectorType = vectorMapping.VectorType;

        List<Expression> newArguments =

View on GitHub (pinned to 3a2006ef56)