dotnet/efcore · error · ArgumentException

The provided DbFunction expression

Error message

The provided DbFunction expression '{expression}' is invalid. The expression must be a lambda expression containing a single method call to the target static method. Default values can be provided as arguments if required, e.g. '() => SomeClass.SomeMethod(null, 0)'.

What it means

HasDbFunction expects a lambda whose Body is a MethodCallExpression referencing the target static method. If the lambda body is anything else (a property access, a constructor, a member expression, etc.), methodInfo resolves to null and EF Core throws ArgumentException with a hint to supply default argument values.

Solutions

  1. Ensure the lambda body is a single call to the target static method, e.g. () => SomeClass.SomeMethod(null, 0).
  2. Provide default placeholder values for all parameters including optional/nullable ones.
  3. If the target is a property, expose it via a static method wrapper first.
  4. Use the MethodInfo overload of HasDbFunction if constructing the lambda is awkward.

Example fix

// before
modelBuilder.HasDbFunction(() => MyFunctions.ComputeFactor);
// after
modelBuilder.HasDbFunction(() => MyFunctions.ComputeFactor(null, 0));
Defensive patterns

Strategy: validation

Validate before calling

// Validate the lambda body is a MethodCallExpression before calling HasDbFunction.
if (expression.Body is not MethodCallExpression mce)
    throw new ArgumentException("Expression must be a method call.");
modelBuilder.HasDbFunction(mce.Method);

Type guard

static bool IsMethodCallLambda<T>(Expression<Func<T>> e)
    => e.Body is MethodCallExpression;

Prevention

When it happens

Trigger: Passing an Expression<Func<T>> whose Body is not a MethodCallExpression — e.g. () => SomeClass.Property, () => new Thing(), or a lambda wrapping a field rather than a method call.

Common situations: Refactoring a static method into a property and forgetting to update the HasDbFunction lambda; pointing HasDbFunction at an instance method; copy-pasting a lambda that references the wrong member; missing default arguments for nullable/optional parameters.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Extensions/RelationalModelBuilderExtensions.cs:298

    /// <summary>
    ///     Configures a database function when targeting a relational database.
    /// </summary>
    /// <remarks>
    ///     See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see> for more information and examples.
    /// </remarks>
    /// <param name="modelBuilder">The model builder.</param>
    /// <param name="expression">The method this dbFunction uses.</param>
    /// <returns>A builder to further configure the function.</returns>
    public static DbFunctionBuilder HasDbFunction<TResult>(
        this ModelBuilder modelBuilder,
        Expression<Func<TResult>> expression)
    {
        Check.NotNull(expression);

        var methodInfo = (expression.Body as MethodCallExpression)?.Method;

        return methodInfo == null
            ? throw new ArgumentException(RelationalStrings.DbFunctionExpressionIsNotMethodCall(expression))
            : modelBuilder.HasDbFunction(methodInfo);
    }

    /// <summary>
    ///     Configures a database function when targeting a relational database.
    /// </summary>
    /// <remarks>
    ///     See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see> for more information and examples.
    /// </remarks>
    /// <param name="modelBuilder">The model builder.</param>
    /// <param name="methodInfo">The methodInfo this dbFunction uses.</param>
    /// <param name="builderAction">An action that performs configuration of the sequence.</param>
    /// <returns>A builder to further configure the function.</returns>
    public static ModelBuilder HasDbFunction(
        this ModelBuilder modelBuilder,
        MethodInfo methodInfo,
        Action<DbFunctionBuilder> builderAction)
    {

View on GitHub (pinned to 3a2006ef56)