{"record":{"id":"e7a8890d34221f63","repo":"dotnet/efcore","slug":"the-provided-dbfunction-expression-expression","errorCode":null,"errorMessage":"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)'.","messagePattern":"The provided DbFunction 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\\)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Extensions/RelationalModelBuilderExtensions.cs","lineNumber":298,"sourceCode":"    /// <summary>\n    ///     Configures a database function when targeting a relational database.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-database-functions\">Database functions</see> for more information and examples.\n    /// </remarks>\n    /// <param name=\"modelBuilder\">The model builder.</param>\n    /// <param name=\"expression\">The method this dbFunction uses.</param>\n    /// <returns>A builder to further configure the function.</returns>\n    public static DbFunctionBuilder HasDbFunction<TResult>(\n        this ModelBuilder modelBuilder,\n        Expression<Func<TResult>> expression)\n    {\n        Check.NotNull(expression);\n\n        var methodInfo = (expression.Body as MethodCallExpression)?.Method;\n\n        return methodInfo == null\n            ? throw new ArgumentException(RelationalStrings.DbFunctionExpressionIsNotMethodCall(expression))\n            : modelBuilder.HasDbFunction(methodInfo);\n    }\n\n    /// <summary>\n    ///     Configures a database function when targeting a relational database.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-database-functions\">Database functions</see> for more information and examples.\n    /// </remarks>\n    /// <param name=\"modelBuilder\">The model builder.</param>\n    /// <param name=\"methodInfo\">The methodInfo this dbFunction uses.</param>\n    /// <param name=\"builderAction\">An action that performs configuration of the sequence.</param>\n    /// <returns>A builder to further configure the function.</returns>\n    public static ModelBuilder HasDbFunction(\n        this ModelBuilder modelBuilder,\n        MethodInfo methodInfo,\n        Action<DbFunctionBuilder> builderAction)\n    {","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Extensions/RelationalModelBuilderExtensions.cs#L280-L316","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the lambda body is a single call to the target static method, e.g. () => SomeClass.SomeMethod(null, 0).","Provide default placeholder values for all parameters including optional/nullable ones.","If the target is a property, expose it via a static method wrapper first.","Use the MethodInfo overload of HasDbFunction if constructing the lambda is awkward."],"exampleFix":"// before\nmodelBuilder.HasDbFunction(() => MyFunctions.ComputeFactor);\n// after\nmodelBuilder.HasDbFunction(() => MyFunctions.ComputeFactor(null, 0));","handlingStrategy":"validation","validationCode":"// Validate the lambda body is a MethodCallExpression before calling HasDbFunction.\nif (expression.Body is not MethodCallExpression mce)\n    throw new ArgumentException(\"Expression must be a method call.\");\nmodelBuilder.HasDbFunction(mce.Method);","typeGuard":"static bool IsMethodCallLambda<T>(Expression<Func<T>> e)\n    => e.Body is MethodCallExpression;","tryCatchPattern":null,"preventionTips":["Always use the form () => StaticClass.Method(defaults) for HasDbFunction lambdas.","Provide explicit default placeholders for every parameter.","Prefer the MethodInfo overload when the lambda is awkward to construct."],"tags":["dbfunction","expression-tree","model-builder","argument-exception"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}