{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/DbFunction.cs#L46-L82","documentation":"Thrown by the DbFunction constructor when the method is non-static AND its declaring type is not assignable to DbContext. EF Core only allows DbFunctions that are static methods or instance methods declared on the DbContext subclass itself, because instance methods need a DbContext to invoke against.","triggerScenarios":"Registering HasDbFunction on an instance method defined on a plain service class, a repository, a static-holder-but-marked-instance helper, or any class that does not inherit from DbContext. Also when passing a MethodInfo whose DeclaringType is a non-DbContext base.","commonSituations":"Developers put a TVF/scalar function on a helper/service class and try to map it. Or refactor a DbContext method out into a partial class that is not a DbContext subclass.","solutions":["Move the method onto the DbContext subclass (or a partial of it that still extends DbContext) and keep it as an instance method.","Make the method static if it does not need DbContext state, then map the static MethodInfo.","If the logic lives in a service, expose a thin DbContext instance/static method that delegates to it, and map that.","Verify DeclaringType.IsAssignableFrom(typeof(DbContext)) before registering."],"exampleFix":"// before\npublic class OrderService\n{\n    public IQueryable<Order> ActiveOrders() => /* ... */;\n}\nmodelBuilder.HasDbFunction(typeof(OrderService).GetMethod(\"ActiveOrders\"));\n\n// after\npublic partial class AppDbContext : DbContext\n{\n    public IQueryable<Order> ActiveOrders() => /* ... */;\n}\nmodelBuilder.HasDbFunction(typeof(AppDbContext).GetMethod(\"ActiveOrders\"));","handlingStrategy":"validation","validationCode":"// Validate the declaring type before registering\nvar method = typeof(OrderService).GetMethod(\"ActiveOrders\");\nif (!method.IsStatic && !typeof(DbContext).IsAssignableFrom(method.DeclaringType))\n    throw new InvalidOperationException(\"DbFunction must be static or on a DbContext subclass.\");","typeGuard":"static bool IsDbFunctionHost(MethodInfo m) => m.IsStatic || typeof(DbContext).IsAssignableFrom(m.DeclaringType);","tryCatchPattern":null,"preventionTips":["Keep DbFunction methods on the DbContext (or a DbContext partial) or make them static.","Do not register repository/service instance methods as DbFunctions.","Use nameof(typeof(AppDbContext)) to bind methods to ensure they live on the context."],"tags":["efcore","dbfunction","model-building","dbcontext"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}