{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/DbFunction.cs#L39-L75","documentation":"Thrown by the DbFunction constructor when the registered MethodInfo.IsGenericMethod is true. EF Core cannot map generic methods to a single database function because each closed generic type would imply a different signature/store function, so it rejects them outright at registration.","triggerScenarios":"Calling HasDbFunction on an open generic method (e.g. a method with a <T> type parameter), or on a method that happens to be generic (HasDbFunction(typeof(Ctx).GetMethod(\"Foo\").MakeGenericMethod(...)) where the method info reports IsGenericMethod).","commonSituations":"Developers try to reuse a generic helper as a DbFunction, or pass a MethodInfo that is still open-generic. Also when reflection picks the generic definition instead of a closed instance.","solutions":["Replace the generic method with one or more non-generic methods, one per closed type you need to map.","If using reflection, close the generic first (MakeGenericMethod(typeof(X))) so IsGenericMethod is false before registering.","Register each concrete overload as a separate DbFunction with a distinct store function name.","Avoid generic methods entirely for DbFunction mappings; map closed concrete methods."],"exampleFix":"// before\npublic IQueryable<T> Find<T>(int id) where T : class\n    => Set<T>().Where(e => EF.Property<int>(e, \"Id\") == id);\nmodelBuilder.HasDbFunction(typeof(Ctx).GetMethod(\"Find\")); // generic\n\n// after\npublic IQueryable<Order> FindOrder(int id) => Orders.Where(o => o.Id == id);\npublic IQueryable<Customer> FindCustomer(int id) => Customers.Where(c => c.Id == id);\nmodelBuilder.HasDbFunction(typeof(Ctx).GetMethod(\"FindOrder\"));\nmodelBuilder.HasDbFunction(typeof(Ctx).GetMethod(\"FindCustomer\"));","handlingStrategy":"validation","validationCode":"// Validate the method is non-generic before registering\nvar method = typeof(Ctx).GetMethod(\"Foo\");\nif (method is null || method.IsGenericMethod)\n    throw new InvalidOperationException(\"DbFunction must be a non-generic method.\");\nmodelBuilder.HasDbFunction(method);","typeGuard":"static bool IsMappableDbFunction(MethodInfo m) => !m.IsGenericMethod;","tryCatchPattern":null,"preventionTips":["Never register open generic methods as DbFunctions.","If you need type variants, write explicit non-generic methods per closed type.","When using reflection, close generics with MakeGenericMethod before registering."],"tags":["efcore","dbfunction","generics","model-building"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}