dotnet/efcore · error · InvalidOperationException
The entity type ' ' is mapped to the 'DbFunction' named ' '…
Error message
The entity type '{entityType}' is mapped to the 'DbFunction' named '{functionName}' with return type '{returnType}'. Ensure that the mapped function returns 'IQueryable<{clrType}>'. What it means
Thrown in ValidateDbFunctionMapping when the function an entity is mapped to either is scalar (dbFunction.IsScalar) or its element type (ReturnType.GetGenericArguments()[0]) does not equal entityType.ClrType. See RelationalModelValidator.cs:444-453. The DbFunction must return IQueryable<TExactClrType> matching the entity.
Solutions
- Make the DbFunction return IQueryable<TEntity> where TEntity is exactly the entity's CLR type.
- Point .ToFunction at the correct IQueryable<T> DbFunction (verify by name).
- If the function is meant to be scalar-only, remove the entity's .ToFunction mapping.
- Re-run modelBuilder.HasDbFunction(() => TheMethod()) so the cached method signature is picked up.
Example fix
// before — scalar return + entity mapping mismatch
public int CountOrdersForCustomer(int customerId) => throw new NotImplementedException();
modelBuilder.HasDbFunction(() => CountOrdersForCustomer(default));
modelBuilder.Entity<Order>().ToFunction("CountOrdersForCustomer");
// after — TVF returning the entity
public IQueryable<Order> OrdersForCustomer(int customerId)
=> FromExpression(() => OrdersForCustomer(customerId));
modelBuilder.HasDbFunction(() => OrdersForCustomer(default));
modelBuilder.Entity<Order>().ToFunction("OrdersForCustomer"); Defensive patterns
Strategy: validation
Validate before calling
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
var fnName = et.GetFunctionName();
if (fnName is null) continue;
var fn = et.Model.FindDbFunction(fnName);
if (fn is null) continue;
if (fn.IsScalar || fn.ReturnType.GetGenericArguments()[0] != et.ClrType)
throw new InvalidOperationException($"DbFunction '{fnName}' must return IQueryable<{et.ClrType.Name}> to map entity '{et.Name}'.");
} Try / catch
try { _ = ctx.Model; }
catch (InvalidOperationException ex) when (ex.Message.Contains("Ensure that the mapped function returns"))
{
logger.LogError("Return-type mismatch between entity and DbFunction. {Msg}", ex.Message);
throw;
} Prevention
- Keep the DbFunction method signature's generic argument aligned with the entity type via FromExpression(() => ...).
- Unit-test the model build after signature refactors.
- Don't reuse a scalar DbFunction for entity mapping.
When it happens
Trigger: Calling .ToFunction on an entity while the registered DbFunction returns a scalar (int, string) or returns IQueryable<OtherType> where OtherType != the entity's CLR type. Common after refactoring a method signature or pointing ToFunction at the wrong DbFunction.
Common situations: Registering a scalar-valued function and then trying to map an entity to it; changing the DbFunction method's generic argument during a refactor; mixing up two similarly-named DbFunctions in the model.
Related errors
- The DbFunction ' ' returns ' ', but ' ' is not a mapped…
- The entity type ' ' is mapped to the 'DbFunction' named '…
- The entity type ' ' is mapped to the 'DbFunction' named ' '…
- The entity type ' ' is mapped to the DbFunction named ' '…
- Both entity type ' ' and ' ' were configured to use ' '…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/3e05c5588aec53fc.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:447
var mappedFunction = entityType.Model.FindDbFunction(mappedFunctionName);
if (mappedFunction == null)
{
throw new InvalidOperationException(
RelationalStrings.MappedFunctionNotFound(entityType.DisplayName(), mappedFunctionName));
}
if (entityType.BaseType != null)
{
throw new InvalidOperationException(
RelationalStrings.InvalidMappedFunctionDerivedType(
entityType.DisplayName(), mappedFunctionName, entityType.BaseType.DisplayName()));
}
if (mappedFunction.IsScalar
|| mappedFunction.ReturnType.GetGenericArguments()[0] != entityType.ClrType)
{
throw new InvalidOperationException(
RelationalStrings.InvalidMappedFunctionUnmatchedReturn(
entityType.DisplayName(),
mappedFunctionName,
mappedFunction.ReturnType.ShortDisplayName(),
entityType.ClrType.ShortDisplayName()));
}
if (mappedFunction.Parameters.Count > 0)
{
var parameters = "{"
+ string.Join(
", ",
mappedFunction.Parameters.Select(p => "'" + p.Name + "'"))
+ "}";
throw new InvalidOperationException(
RelationalStrings.InvalidMappedFunctionWithParameters(
entityType.DisplayName(), mappedFunctionName, parameters));
}View on GitHub (pinned to 3a2006ef56)