dotnet/efcore · error · ArgumentException

The DbFunction ' ' is generic. Mapping generic methods as a…

Error message

The DbFunction '{function}' is generic. Mapping generic methods as a DbFunction is not supported.

What it means

EF Core's DbFunction mapping does not support generic methods, because each generic instantiation would need a separate SQL function mapping and EF has no way to bind the type parameters to a concrete database function. The DbFunction constructor throws an ArgumentException when methodInfo.IsGenericMethod is true.

Solutions

  1. Replace the generic method with a concrete, non-generic method whose parameter and return types are fully specified for each TVF/scalar function you map.
  2. Register each concrete overload separately with HasDbFunction if you need multiple shapes.
  3. Keep generic helpers out of the DbFunction pipeline; they can wrap the mapped concrete methods externally.

Example fix

// before
public IQueryable<T> GetThings<T>() => throw new NotSupportedException();
modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(GetThings)));

// after
public IQueryable<Thing> GetThings() => throw new NotSupportedException();
modelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(GetThings)));
Defensive patterns

Strategy: validation

Validate before calling

var method = typeof(MyContext).GetMethod(nameof(MyContext.GetThings));
if (method is not null && method.IsGenericMethod)
{
    throw new InvalidOperationException("Cannot register generic method " + method.Name + " as a DbFunction.");
}
modelBuilder.HasDbFunction(method!);

Type guard

static bool IsNonGenericDbFunctionCandidate(MethodInfo m)
    => !m.IsGenericMethod && m.ReturnType != typeof(void);

Prevention

When it happens

Trigger: Calling modelBuilder.HasDbFunction on a method declared with generic type parameters (e.g. IQueryable<T> Foo<T>()). Applies to both instance methods on the DbContext and static helper methods.

Common situations: Reusing a generic repository helper as a DbFunction; refactoring a strongly-typed method to generic without realizing DbFunction requires concrete types.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Metadata/Internal/DbFunction.cs:57

    ///     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(
        MethodInfo methodInfo,
        IMutableModel model,
        ConfigurationSource configurationSource)
        : this(
            methodInfo.Name,
            methodInfo.ReturnType,
            methodInfo.GetParameters().Select(pi => (pi.Name!, pi.ParameterType)),
            model,
            configurationSource)
    {
        if (methodInfo.IsGenericMethod)
        {
            throw new ArgumentException(RelationalStrings.DbFunctionGenericMethodNotSupported(methodInfo.DisplayName()));
        }

        if (!methodInfo.IsStatic
            && !typeof(DbContext).IsAssignableFrom(methodInfo.DeclaringType))
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            throw new ArgumentException(
                RelationalStrings.DbFunctionInvalidInstanceType(
                    methodInfo.DisplayName(), methodInfo.DeclaringType!.ShortDisplayName()));
        }

        MethodInfo = methodInfo;

        ModelName = GetFunctionName(methodInfo);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to

View on GitHub (pinned to 3a2006ef56)