dotnet/efcore · error · InvalidOperationException
The '{methodName}' method is not supported because the query
Error message
The '{methodName}' method is not supported because the query has switched to client-evaluation. This usually happens when the arguments to the method cannot be translated to server. Rewrite the query to avoid client evaluation of arguments so that method can be translated to server. What it means
EF.Functions.IsDefined is a translation-only stub: its method body always throws InvalidOperationException. It exists solely to be translated into Cosmos's IS_DEFINED SQL function inside an IQueryable LINQ query. If the query falls back to client evaluation (or the method is invoked directly), the throw fires because there is no real client-side implementation.
Source
Thrown at src/EFCore.Cosmos/Extensions/CosmosDbFunctionsExtensions.cs:28
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Cosmos with EF Core</see> for more information and examples.
/// </remarks>
public static class CosmosDbFunctionsExtensions
{
/// <summary>
/// Returns a boolean indicating if the property has been assigned a value. Corresponds to the Cosmos <c>IS_DEFINED</c> function.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Cosmos with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="expression">The expression to check.</param>
/// <seealso href="https://learn.microsoft.com/azure/cosmos-db/nosql/query/is-defined">Cosmos <c>IS_DEFINED_</c> function</seealso>
public static bool IsDefined(this DbFunctions _, object? expression)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(IsDefined)));
/// <summary>
/// Coalesces a Cosmos <c>undefined</c> value via the <c>??</c> operator.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Cosmos with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="expression1">
/// The expression to coalesce. This expression will be returned unless it is <c>undefined</c>, in which case
/// <paramref name="expression2" /> will be returned.
/// </param>
/// <param name="expression2">The expression to be returned if <paramref name="expression1" /> is <c>undefined</c>.</param>
/// <seealso href="https://learn.microsoft.com/azure/cosmos-db/nosql/query/ternary-coalesce-operators#coalesce-operator">
/// Cosmos coalesce operator
/// </seealso>View on GitHub (pinned to dbf9771522)
Solutions
- Use EF.Functions.IsDefined only inside an IQueryable query against a Cosmos-backed DbContext so EF translates it to IS_DEFINED.
- Ensure all arguments to IsDefined are properties/columns of the entity, not opaque client values.
- If client evaluation is unavoidable, replace the call with a translatable equivalent (e.g., a null/has-value check the provider can translate).
Example fix
// before var has = EF.Functions.IsDefined(localVar); // direct call -> throws // or query with non-translatable arg forcing client eval // after var q = db.Blogs.Where(b => EF.Functions.IsDefined(b.OptionalField)); // translated to IS_DEFINED on server
Defensive patterns
Strategy: validation
Validate before calling
// Ensure the call is inside a translatable IQueryable, not invoked directly
// There is no runtime guard for 'is this a query'; structurally avoid direct calls:
if (query is not IQueryable)
{
throw new InvalidOperationException("EF.Functions.IsDefined must be used inside an IQueryable query.");
} Prevention
- Only use EF.Functions.IsDefined inside IQueryable queries over a Cosmos-backed DbSet.
- Keep arguments as entity properties/columns so they translate and don't force client evaluation.
- Never call EF.Functions methods directly or against IEnumerable.
When it happens
Trigger: Calling EF.Functions.IsDefined(x) outside of a query (direct invocation), or using it inside a query where one of the arguments cannot be translated, forcing EF Core to evaluate that part on the client.
Common situations: Passing a client-side variable or non-translatable expression as the argument. Using IsDefined in LINQ-to-Objects (IEnumerable) instead of IQueryable against the DbSet. Upgrading/misconfiguring the Cosmos provider so translation is missing. Calling the method in a unit test without a query provider.
Related errors
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/81de7d2340252e6c.
Report an issue: GitHub.