dotnet/efcore · error · ArgumentException
The DbFunction '{function}' defined on type '{type}' must be
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
Thrown by the DbFunction constructor when the method is non-static AND its declaring type is not assignable to DbContext. EF Core only allows DbFunctions that are static methods or instance methods declared on the DbContext subclass itself, because instance methods need a DbContext to invoke against.
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 dbf9771522)
Solutions
- Move the method onto the DbContext subclass (or a partial of it that still extends DbContext) and keep it as an instance method.
- Make the method static if it does not need DbContext state, then map the static MethodInfo.
- If the logic lives in a service, expose a thin DbContext instance/static method that delegates to it, and map that.
- Verify DeclaringType.IsAssignableFrom(typeof(DbContext)) before registering.
Example fix
// before
public class OrderService
{
public IQueryable<Order> ActiveOrders() => /* ... */;
}
modelBuilder.HasDbFunction(typeof(OrderService).GetMethod("ActiveOrders"));
// after
public partial class AppDbContext : DbContext
{
public IQueryable<Order> ActiveOrders() => /* ... */;
}
modelBuilder.HasDbFunction(typeof(AppDbContext).GetMethod("ActiveOrders")); Defensive patterns
Strategy: validation
Validate before calling
// Validate the declaring type before registering
var method = typeof(OrderService).GetMethod("ActiveOrders");
if (!method.IsStatic && !typeof(DbContext).IsAssignableFrom(method.DeclaringType))
throw new InvalidOperationException("DbFunction must be static or on a DbContext subclass."); Type guard
static bool IsDbFunctionHost(MethodInfo m) => m.IsStatic || typeof(DbContext).IsAssignableFrom(m.DeclaringType);
Prevention
- Keep DbFunction methods on the DbContext (or a DbContext partial) or make them static.
- Do not register repository/service instance methods as DbFunctions.
- Use nameof(typeof(AppDbContext)) to bind methods to ensure they live on the context.
When it happens
Trigger: Registering HasDbFunction on an instance method defined on a plain service class, a repository, a static-holder-but-marked-instance helper, or any class that does not inherit from DbContext. Also when passing a MethodInfo whose DeclaringType is a non-DbContext base.
Common situations: Developers put a TVF/scalar function on a helper/service class and try to map it. Or refactor a DbContext method out into a partial class that is not a DbContext subclass.
Related errors
- The DbFunction '{function}' has an invalid return type '{typ
- The DbFunction '{function}' is generic. Mapping generic meth
- The DbFunction '{function}' has an invalid return type '{typ
- '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/82557532cdfcdfa6.
Report an issue: GitHub.