dotnet/efcore · error · ArgumentException

The DbFunction ' ' has an invalid return type ' '. Ensure…

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

A DbFunction must have a non-null, non-void return type so the provider can map it. The second DbFunction constructor throws when returnType is null or typeof(void), because there is nothing for the database function to return that EF could materialize.

Solutions

  1. Change the method so it returns a scalar type or IQueryable<T> consistent with the database function's return shape.
  2. If the method represents a stored procedure with no tabular result, use modelBuilder.HasStoredProcedure(...) instead of HasDbFunction.
  3. Re-register the corrected method with HasDbFunction.

Example fix

// before
public void Recalculate() { /* ... */ }
modelBuilder.HasDbFunction(typeof(AppDb).GetMethod(nameof(Recalculate)));

// after
public int Recalculate() => throw new NotSupportedException();
modelBuilder.HasDbFunction(typeof(AppDb).GetMethod(nameof(Recalculate)));
Defensive patterns

Strategy: validation

Validate before calling

var method = typeof(AppDb).GetMethod(nameof(AppDb.Recalculate));
if (method is null || method.ReturnType is null || method.ReturnType == typeof(void))
{
    throw new InvalidOperationException("DbFunction must return a non-void, mappable type.");
}

Type guard

static bool HasMappableReturnType(MethodInfo m)
    => m.ReturnType is not null && m.ReturnType != typeof(void);

Prevention

When it happens

Trigger: Registering a method whose declared return type is void (a procedure-style method) or passing null explicitly to the DbFunction constructor's returnType parameter.

Common situations: Wrapping a stored procedure as a DbFunction (use HasStoredProcedure instead for procedures); refactor that changed a method's return type to void; misconfigured method signature.

Related errors


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

Appendix: 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 3a2006ef56)