dotnet/efcore · error · InvalidOperationException
The DbFunction '{function}' has an invalid return type '{typ
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
ValidateDbFunction (RelationalModelValidator.cs:363-371) throws for a SCALAR DbFunction whose TypeMapping is null - meaning the current provider has no relational type mapping for the function's return type. A scalar DbFunction must return a value the provider can map (int, string, DateTime, etc.); an unmappable return type cannot be translated to a SQL scalar, so the function registration is rejected.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:367
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
}
/// <summary>
/// Validates a single database function.
/// </summary>
/// <param name="dbFunction">The database function to validate.</param>
/// <param name="logger">The logger to use.</param>
protected virtual void ValidateDbFunction(
IDbFunction dbFunction,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
var model = dbFunction.Model;
if (dbFunction.IsScalar)
{
if (dbFunction.TypeMapping == null)
{
throw new InvalidOperationException(
RelationalStrings.DbFunctionInvalidReturnType(
dbFunction.ModelName,
dbFunction.ReturnType.ShortDisplayName()));
}
}
else
{
var elementType = dbFunction.ReturnType.GetGenericArguments()[0];
var entityType = model.FindEntityType(elementType);
if (entityType?.IsOwned() == true
|| ((IConventionModel)model).IsOwned(elementType)
|| (entityType == null && model.GetEntityTypes().Any(e => e.ClrType == elementType)))
{
throw new InvalidOperationException(
RelationalStrings.DbFunctionInvalidIQueryableOwnedReturnType(
dbFunction.ModelName, elementType.ShortDisplayName()));
}View on GitHub (pinned to dbf9771522)
Solutions
- Change the DbFunction return type to one the provider maps natively (int, string, DateTime, decimal, etc.).
- If the type is custom/an enum, configure a value converter so it maps to a primitive the provider understands.
- Register a custom relational type mapping for the return type via the provider's plugin if you control the provider.
Example fix
// before public MyCustomStruct ComputeScore(int id) => default; modelBuilder.HasDbFunction(() => ComputeScore(default)); // unmappable return -> throws // after - return a mapped primitive (or convert the custom type) public int ComputeScore(int id) => 0; modelBuilder.HasDbFunction(() => ComputeScore(default));
Defensive patterns
Strategy: validation
Validate before calling
// Pre-validate scalar DbFunction return types are provider-mapped.
foreach (var fn in context.Model.GetDbFunctions())
{
if (fn.IsScalar && fn.TypeMapping == null)
{
// will throw - change the return type or add a value converter/mapping.
}
} Prevention
- Use provider-mappable primitive return types for scalar DbFunctions.
- Configure value conversions for enum/custom return types.
- Check provider type-coverage tables before adding exotic return types.
When it happens
Trigger: Registering a DbFunction via HasDbFunction() whose CLR return type is not supported by the provider (e.g. a custom struct/enum without a mapping, or an unsupported type like IntPtr/Guid on a limited provider). Thrown at model validation.
Common situations: Custom value types as DbFunction returns; enums not configured with HasConversion; using a type the provider doesn't map (e.g. certain providers and char/TimeSpan variants); porting DbFunctions between providers with different type coverage.
Related errors
- The parameter '{parameter}' for the DbFunction '{function}'
- The DbFunction '{function}' has an invalid return type '{typ
- The DbFunction '{function}' returns '{type}', but '{elementT
- The element type of the result of '{dbFunction}' is mapped t
- The entity type '{entityType}' is mapped to the DbFunction n
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/bff5dd2f20ee7cb6.
Report an issue: GitHub.