{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Extensions/RelationalModelBuilderExtensions.cs#L280-L316","documentation":"ModelBuilder.HasDbFunction<TResult>(Expression<Func<TResult>>) requires the lambda body to be a single MethodCallExpression whose Method is the target static method (RelationalModelBuilderExtensions.cs:298, RelationalStrings.DbFunctionExpressionIsNotMethodCall). EF extracts the MethodInfo from expression.Body as MethodCallExpression; any other shape yields null and throws.","triggerScenarios":"Passing a lambda that is a property access, a field, a constructor call, a multi-statement block, a delegate invocation, or a method group rather than a direct call: e.g. () => MyHelpers.Total or () => 1 + Foo() instead of () => Foo(null, 0).","commonSituations":"Registering a DbFunction whose body wraps the call in arithmetic, calls an instance method, or references a property; converting a Func<T> delegate into an expression incorrectly; refactoring the static method signature.","solutions":["Make the lambda body exactly one method call to the target static method, supplying default-typed arguments: () => MyType.MyMethod(null, 0).","Prefer the MethodInfo overload: HasDbFunction(typeof(MyType).GetMethod(nameof(MyType.MyMethod))!).","Ensure the target method is static and on the type registered; remove wrapping/composition from the expression."],"exampleFix":"// before\nmodelBuilder.HasDbFunction(() => MyDb.Foo());           // ok if Foo() is the body\nmodelBuilder.HasDbFunction(() => 1 + MyDb.Foo());        // throws: body is Add, not MethodCall\nmodelBuilder.HasDbFunction(() => MyDb.Value);            // throws: body is MemberAccess\n\n// after\nmodelBuilder.HasDbFunction(() => MyDb.Foo(null, 0));\n// or\nmodelBuilder.HasDbFunction(typeof(MyDb).GetMethod(nameof(MyDb.Foo))!);","handlingStrategy":"validation","validationCode":"// Validate the lambda body is a single method call before registering.\nExpression<Func<object>> expr = () => MyType.MyMethod(null, 0);\nif (expr.Body is not MethodCallExpression mce)\n    throw new ArgumentException(\"DbFunction expression must be a single method call.\");\nmodelBuilder.HasDbFunction(mce.Method);","typeGuard":"static bool IsMethodCallLambda<T>(Expression<Func<T>> e) => e.Body is MethodCallExpression;","tryCatchPattern":null,"preventionTips":["Prefer the MethodInfo overload of HasDbFunction to avoid expression-shape pitfalls.","Keep the lambda body a bare call with default-typed arguments.","Ensure the target method is static."],"tags":["dbfunction","model-building","expression-trees"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}