{"record":{"id":"f9e408d640752f73","repo":"dotnet/efcore","slug":"the-dbfunction-function-is-generic-mapping-ge","errorCode":null,"errorMessage":"The DbFunction '{function}' is generic. Mapping generic methods as a DbFunction is not supported.","messagePattern":"The DbFunction '(.+?)' is generic\\. Mapping generic methods as a DbFunction is not supported\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/DbFunction.cs","lineNumber":57,"sourceCode":"    ///     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        MethodInfo methodInfo,\n        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","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/DbFunction.cs#L39-L75","documentation":"EF Core's DbFunction mapping does not support generic methods, because each generic instantiation would need a separate SQL function mapping and EF has no way to bind the type parameters to a concrete database function. The DbFunction constructor throws an ArgumentException when methodInfo.IsGenericMethod is true.","triggerScenarios":"Calling modelBuilder.HasDbFunction on a method declared with generic type parameters (e.g. IQueryable<T> Foo<T>()). Applies to both instance methods on the DbContext and static helper methods.","commonSituations":"Reusing a generic repository helper as a DbFunction; refactoring a strongly-typed method to generic without realizing DbFunction requires concrete types.","solutions":["Replace the generic method with a concrete, non-generic method whose parameter and return types are fully specified for each TVF/scalar function you map.","Register each concrete overload separately with HasDbFunction if you need multiple shapes.","Keep generic helpers out of the DbFunction pipeline; they can wrap the mapped concrete methods externally."],"exampleFix":"// before\npublic IQueryable<T> GetThings<T>() => throw new NotSupportedException();\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(GetThings)));\n\n// after\npublic IQueryable<Thing> GetThings() => throw new NotSupportedException();\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(GetThings)));","handlingStrategy":"validation","validationCode":"var method = typeof(MyContext).GetMethod(nameof(MyContext.GetThings));\nif (method is not null && method.IsGenericMethod)\n{\n    throw new InvalidOperationException(\"Cannot register generic method \" + method.Name + \" as a DbFunction.\");\n}\nmodelBuilder.HasDbFunction(method!);","typeGuard":"static bool IsNonGenericDbFunctionCandidate(MethodInfo m)\n    => !m.IsGenericMethod && m.ReturnType != typeof(void);","tryCatchPattern":null,"preventionTips":["Audit every method you pass to HasDbFunction for generic type parameters.","If multiple shapes are needed, declare and register concrete non-generic overloads instead."],"tags":["relational","dbfunctions","model-building","reflection"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}