dotnet/efcore · error · ArgumentException
The DbFunction '{function}' has an invalid return type '{typ
Error message
The DbFunction '{function}' has an invalid return type '{type}'. Ensure that the return type can be mapped by the current provider. What it means
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.
Source
Thrown at src/EFCore.Relational/Metadata/Internal/DbFunction.cs:90
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public DbFunction(
string name,
Type returnType,
IEnumerable<(string Name, Type Type)>? parameters,
IMutableModel model,
ConfigurationSource configurationSource)
{
if (returnType == null
|| returnType == typeof(void))
{
throw new ArgumentException(
RelationalStrings.DbFunctionInvalidReturnType(name, returnType?.ShortDisplayName()));
}
IsScalar = !returnType.IsGenericType
|| returnType.GetGenericTypeDefinition() != typeof(IQueryable<>);
IsAggregate = false;
ModelName = name;
ReturnType = returnType;
Model = model;
_configurationSource = configurationSource;
_builder = new InternalDbFunctionBuilder(this, ((IConventionModel)model).Builder);
_parameters = parameters == null
? []
: parameters
.Select(p => new DbFunctionParameter(this, p.Name, p.Type))
.ToList();
View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
public void ApplyDiscount(int orderId) { /* proc */ }
modelBuilder.HasDbFunction(typeof(Ctx).GetMethod("ApplyDiscount")); // void
// after
public int ApplyDiscount(int orderId) => /* scalar result */ 0;
modelBuilder.HasDbFunction(typeof(Ctx).GetMethod("ApplyDiscount"));
// or for a stored procedure, drop HasDbFunction and call:
// dbContext.Database.ExecuteSqlInterpolated($"EXEC ApplyDiscount {orderId}"); Defensive patterns
Strategy: validation
Validate before calling
// Validate the return type before registering
var rt = method.ReturnType;
if (rt is null || rt == typeof(void))
throw new InvalidOperationException("DbFunction must return a mappable scalar or IQueryable<T>.");
modelBuilder.HasDbFunction(method); Type guard
static bool HasMappableReturn(MethodInfo m) => m.ReturnType is not null && m.ReturnType != typeof(void));
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The DbFunction '{function}' has an invalid return type '{typ
- The DbFunction '{function}' is generic. Mapping generic meth
- The DbFunction '{function}' defined on type '{type}' must be
- 'IsNullable' cannot be set on DbFunction '{functionName}' si
- Custom translation cannot be set on the DbFunction '{functio
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/582925609178266d.
Report an issue: GitHub.