{"record":{"id":"82557532cdfcdfa6","repo":"dotnet/efcore","slug":"the-dbfunction-function-defined-on-type-type","errorCode":null,"errorMessage":"The DbFunction '{function}' defined on type '{type}' must be either a static method or an instance method defined on a DbContext subclass. Instance methods on other types are not supported.","messagePattern":"The DbFunction '(.+?)' defined on type '(.+?)' must be either a static method or an instance method defined on a DbContext subclass\\. Instance methods on other types are not supported\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/DbFunction.cs","lineNumber":64,"sourceCode":"        IMutableModel model,\n        ConfigurationSource configurationSource)\n        : this(\n            methodInfo.Name,\n            methodInfo.ReturnType,\n            methodInfo.GetParameters().Select(pi => (pi.Name!, pi.ParameterType)),\n            model,\n            configurationSource)\n    {\n        if (methodInfo.IsGenericMethod)\n        {\n            throw new ArgumentException(RelationalStrings.DbFunctionGenericMethodNotSupported(methodInfo.DisplayName()));\n        }\n\n        if (!methodInfo.IsStatic\n            && !typeof(DbContext).IsAssignableFrom(methodInfo.DeclaringType))\n        {\n            // ReSharper disable once AssignNullToNotNullAttribute\n            throw new ArgumentException(\n                RelationalStrings.DbFunctionInvalidInstanceType(\n                    methodInfo.DisplayName(), methodInfo.DeclaringType!.ShortDisplayName()));\n        }\n\n        MethodInfo = methodInfo;\n\n        ModelName = GetFunctionName(methodInfo);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public DbFunction(\n        string name,\n        Type returnType,","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/DbFunction.cs#L46-L82","documentation":"A DbFunction method must be either static or an instance method on a class derived from DbContext — instance methods on arbitrary types cannot be invoked by EF when translating the function call into SQL. The constructor throws when the method is non-static and its declaring type is not assignable to DbContext.","triggerScenarios":"Registering HasDbFunction for a method defined on a plain service class, a repository, or a helper that is not a DbContext subclass. The check is !methodInfo.IsStatic && !typeof(DbContext).IsAssignableFrom(methodInfo.DeclaringType).","commonSituations":"Moving a query method out of the DbContext into a separate service and forgetting to make it static; registering a method on an injected helper as a DbFunction; refactor that changed the declaring type.","solutions":["Move the method into your DbContext-derived class as an instance method, OR make the method static (and reference it from anywhere).","Re-register with HasDbFunction pointing at the corrected MethodInfo.","Keep the method non-generic with a mappable return type after the move."],"exampleFix":"// before\npublic class OrderService\n{\n    public IQueryable<Order> ActiveOrders() => throw new NotSupportedException();\n}\nmodelBuilder.HasDbFunction(typeof(OrderService).GetMethod(nameof(OrderService.ActiveOrders)));\n\n// after (move into DbContext)\npublic class AppDb : DbContext\n{\n    public IQueryable<Order> ActiveOrders() => throw new NotSupportedException();\n}\nmodelBuilder.HasDbFunction(typeof(AppDb).GetMethod(nameof(AppDb.ActiveOrders)));","handlingStrategy":"validation","validationCode":"var method = typeof(OrderService).GetMethod(nameof(OrderService.ActiveOrders));\nif (method is not null\n    && !method.IsStatic\n    && !typeof(DbContext).IsAssignableFrom(method.DeclaringType))\n{\n    throw new InvalidOperationException(\"DbFunction must be static or instance on a DbContext subclass.\");\n}","typeGuard":"static bool IsDbFunctionHost(MethodInfo m)\n    => m.IsStatic || typeof(DbContext).IsAssignableFrom(m.DeclaringType);","tryCatchPattern":null,"preventionTips":["Keep DbFunction methods inside your DbContext subclass, or make them static.","Avoid registering methods defined on services or repositories as DbFunctions."],"tags":["relational","dbfunctions","model-building","dbcontext"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}