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

  1. Change the method to return a mappable type: a scalar (int, string, decimal, ...) or IQueryable<TEntity>.
  2. If you actually need a stored procedure, do not use HasDbFunction — execute it via FromSqlInterpolated/ExecuteSqlInterpolated or raw SQL instead.
  3. If using the string-based DbFunction constructor, pass a non-null, non-void return type.
  4. 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

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


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/582925609178266d. Report an issue: GitHub.