{"record":{"id":"582925609178266d","repo":"dotnet/efcore","slug":"the-dbfunction-function-has-an-invalid-return-582925","errorCode":null,"errorMessage":"The DbFunction '{function}' has an invalid return type '{type}'. Ensure that the return type can be mapped by the current provider.","messagePattern":"The DbFunction '(.+?)' has an invalid return type '(.+?)'\\. Ensure that the return type can be mapped by the current provider\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/DbFunction.cs","lineNumber":90,"sourceCode":"    }\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,\n        IEnumerable<(string Name, Type Type)>? parameters,\n        IMutableModel model,\n        ConfigurationSource configurationSource)\n    {\n        if (returnType == null\n            || returnType == typeof(void))\n        {\n            throw new ArgumentException(\n                RelationalStrings.DbFunctionInvalidReturnType(name, returnType?.ShortDisplayName()));\n        }\n\n        IsScalar = !returnType.IsGenericType\n            || returnType.GetGenericTypeDefinition() != typeof(IQueryable<>);\n        IsAggregate = false;\n\n        ModelName = name;\n        ReturnType = returnType;\n        Model = model;\n        _configurationSource = configurationSource;\n        _builder = new InternalDbFunctionBuilder(this, ((IConventionModel)model).Builder);\n        _parameters = parameters == null\n            ? []\n            : parameters\n                .Select(p => new DbFunctionParameter(this, p.Name, p.Type))\n                .ToList();\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/DbFunction.cs#L72-L108","documentation":"Thrown by the DbFunction constructor when the return type is null or typeof(void). A DbFunction must return a value that can be mapped by the provider (a scalar type or an IQueryable<T>); void/null return types have nothing to map to a SQL function result.","triggerScenarios":"Registering a method whose return type is void (a procedure-style method) as a DbFunction. Also when constructing a DbFunction via the (name, returnType, ...) overload with returnType=null. Common with stored-procedure wrappers that have no return value.","commonSituations":"Trying to map a stored procedure (which has no scalar return) using HasDbFunction — EF Core DbFunctions are for scalar functions or TVFs, not procedures. Misusing the lower-level DbFunction constructor with a null type.","solutions":["Change the method to return a mappable type: a scalar (int, string, decimal, ...) or IQueryable<TEntity>.","If you actually need a stored procedure, do not use HasDbFunction — execute it via FromSqlInterpolated/ExecuteSqlInterpolated or raw SQL instead.","If using the string-based DbFunction constructor, pass a non-null, non-void return type.","Double-check the MethodInfo passed to HasDbFunction actually returns a value."],"exampleFix":"// before\npublic void ApplyDiscount(int orderId) { /* proc */ }\nmodelBuilder.HasDbFunction(typeof(Ctx).GetMethod(\"ApplyDiscount\")); // void\n\n// after\npublic int ApplyDiscount(int orderId) => /* scalar result */ 0;\nmodelBuilder.HasDbFunction(typeof(Ctx).GetMethod(\"ApplyDiscount\"));\n// or for a stored procedure, drop HasDbFunction and call:\n// dbContext.Database.ExecuteSqlInterpolated($\"EXEC ApplyDiscount {orderId}\");","handlingStrategy":"validation","validationCode":"// Validate the return type before registering\nvar rt = method.ReturnType;\nif (rt is null || rt == typeof(void))\n    throw new InvalidOperationException(\"DbFunction must return a mappable scalar or IQueryable<T>.\");\nmodelBuilder.HasDbFunction(method);","typeGuard":"static bool HasMappableReturn(MethodInfo m) => m.ReturnType is not null && m.ReturnType != typeof(void));","tryCatchPattern":null,"preventionTips":["Do not map void/stored-procedure methods via HasDbFunction; use raw SQL instead.","Verify the method's return type is a scalar provider-supported type or IQueryable<TEntity>.","When using the string-based DbFunction constructor, always pass a non-null return type."],"tags":["efcore","dbfunction","model-building","stored-procedure"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}