dotnet/efcore · error · ArgumentException

The DbFunction ' ' defined on type ' ' must be either a…

Error message

The DbFunction '{function}' defined on type '{type}' must be either a static method or an instance method defined on a DbContext subclass. Instance methods on other types are not supported.

What it means

A DbFunction method must be either static or an instance method on a class derived from DbContext — instance methods on arbitrary types cannot be invoked by EF when translating the function call into SQL. The constructor throws when the method is non-static and its declaring type is not assignable to DbContext.

Solutions

  1. Move the method into your DbContext-derived class as an instance method, OR make the method static (and reference it from anywhere).
  2. Re-register with HasDbFunction pointing at the corrected MethodInfo.
  3. Keep the method non-generic with a mappable return type after the move.

Example fix

// before
public class OrderService
{
    public IQueryable<Order> ActiveOrders() => throw new NotSupportedException();
}
modelBuilder.HasDbFunction(typeof(OrderService).GetMethod(nameof(OrderService.ActiveOrders)));

// after (move into DbContext)
public class AppDb : DbContext
{
    public IQueryable<Order> ActiveOrders() => throw new NotSupportedException();
}
modelBuilder.HasDbFunction(typeof(AppDb).GetMethod(nameof(AppDb.ActiveOrders)));
Defensive patterns

Strategy: validation

Validate before calling

var method = typeof(OrderService).GetMethod(nameof(OrderService.ActiveOrders));
if (method is not null
    && !method.IsStatic
    && !typeof(DbContext).IsAssignableFrom(method.DeclaringType))
{
    throw new InvalidOperationException("DbFunction must be static or instance on a DbContext subclass.");
}

Type guard

static bool IsDbFunctionHost(MethodInfo m)
    => m.IsStatic || typeof(DbContext).IsAssignableFrom(m.DeclaringType);

Prevention

When it happens

Trigger: Registering HasDbFunction for a method defined on a plain service class, a repository, or a helper that is not a DbContext subclass. The check is !methodInfo.IsStatic && !typeof(DbContext).IsAssignableFrom(methodInfo.DeclaringType).

Common situations: Moving a query method out of the DbContext into a separate service and forgetting to make it static; registering a method on an injected helper as a DbFunction; refactor that changed the declaring type.

Related errors


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

Appendix: source

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

        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
    ///     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,

View on GitHub (pinned to 3a2006ef56)