{"record":{"id":"738e7247b637bfbc","repo":"dotnet/efcore","slug":"the-parameter-value-passed-to-methodname-m","errorCode":null,"errorMessage":"The '{parameter}' value passed to '{methodName}' must be a constant.","messagePattern":"The '(.+?)' value passed to '(.+?)' must be a constant\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Translators/CosmosVectorSearchTranslator.cs","lineNumber":45,"sourceCode":"        SqlExpression? instance,\n        MethodInfo method,\n        IReadOnlyList<SqlExpression> arguments,\n        IDiagnosticsLogger<DbLoggerCategory.Query> logger)\n    {\n        if (method.DeclaringType != typeof(CosmosDbFunctionsExtensions)\n            || method.Name != nameof(CosmosDbFunctionsExtensions.VectorDistance))\n        {\n            return null;\n        }\n\n        if (arguments is not [_, var vector1, var vector2, var useBruteForceExpression, var optionsExpression])\n        {\n            throw new UnreachableException();\n        }\n\n        if (useBruteForceExpression is not SqlConstantExpression { Value: var useBruteForceValue })\n        {\n            throw new InvalidOperationException(\n                CoreStrings.ArgumentNotConstant(\"useBruteForce\", nameof(CosmosDbFunctionsExtensions.VectorDistance)));\n        }\n\n        if (optionsExpression is not SqlConstantExpression { Value: var optionsValue })\n        {\n            throw new InvalidOperationException(\n                CoreStrings.ArgumentNotConstant(\"options\", nameof(CosmosDbFunctionsExtensions.VectorDistance)));\n        }\n\n        var options = (VectorDistanceOptions?)optionsValue;\n\n        var vectorMapping = vector1.TypeMapping as CosmosVectorTypeMapping\n            ?? vector2.TypeMapping as CosmosVectorTypeMapping\n            ?? throw new InvalidOperationException(CosmosStrings.VectorSearchRequiresVector);\n\n        var vectorType = vectorMapping.VectorType;\n\n        List<Expression> newArguments =","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/Translators/CosmosVectorSearchTranslator.cs#L27-L63","documentation":"The Cosmos DB VectorDistance SQL function requires the 'useBruteForce' argument to be a compile-time constant because EF Core must inline the literal value into the generated SQL at translation time. A non-constant expression (variable, column reference, or computed value) cannot be embedded into the query text. The translator at CosmosVectorSearchTranslator.cs:43-47 checks that the argument is a SqlConstantExpression and throws if it is not.","triggerScenarios":"Calling EF.Functions.VectorDistance(e.Vector, queryVector, bruteForceVar, null) in a LINQ query where bruteForceVar is a runtime variable, method parameter, or any expression EF Core cannot fold to a constant. The 'useBruteForce' parameter is marked [NotParameterized] so it must resolve to a literal.","commonSituations":"Passing a configuration flag read from IConfiguration or appsettings at runtime as the brute-force argument. Using a method-level bool parameter to toggle brute-force mode. Conditionally building the argument with a ternary that EF cannot constant-fold.","solutions":["Pass a literal true, false, or null for the useBruteForce argument instead of a variable.","If the value must vary at runtime, branch into two separate LINQ queries (one with brute-force true, one with false/null) and select between them in C# before executing.","Omit the useBruteForce argument entirely (pass null) to use the default index-based behavior."],"exampleFix":"// before\nvar bruteForce = config.GetValue<bool>(\"Vector:BruteForce\");\nvar result = await db.Items\n    .OrderBy(e => EF.Functions.VectorDistance(e.Embedding, q, bruteForce, null))\n    .FirstAsync();\n\n// after\nvar result = bruteForce\n    ? await db.Items.OrderBy(e => EF.Functions.VectorDistance(e.Embedding, q, true, null)).FirstAsync()\n    : await db.Items.OrderBy(e => EF.Functions.VectorDistance(e.Embedding, q, null, null)).FirstAsync();","handlingStrategy":"validation","validationCode":"// Before building the query, ensure the useBruteForce value is a literal or null.\n// There is no runtime type guard; the constraint is enforced at LINQ translation.\n// Validate at the call site by only passing constants:\nstatic IQueryable<Item> VectorSearch(AppDbContext db, ReadOnlyMemory<float> q, bool? bruteForce)\n    => bruteForce switch\n    {\n        true => db.Items.OrderBy(e => EF.Functions.VectorDistance(e.Embedding, q, true, null)),\n        false => db.Items.OrderBy(e => EF.Functions.VectorDistance(e.Embedding, q, false, null)),\n        null => db.Items.OrderBy(e => EF.Functions.VectorDistance(e.Embedding, q, null, null)),\n    };","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass a runtime variable as the useBruteForce argument to VectorDistance; always inline a literal.","If the value comes from config, branch the LINQ query in C# before execution.","Add a unit/integration test that exercises the vector query to catch translation errors early."],"tags":["cosmos","query-translation","vector-search","linq"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}