dotnet/efcore · error · InvalidOperationException
The parameter '{parameter}' for the DbFunction '{function}'
Error message
The parameter '{parameter}' for the DbFunction '{function}' has an invalid type '{type}'. Ensure the parameter type can be mapped by the current provider. What it means
ValidateDbFunction (RelationalModelValidator.cs:402-412) throws when any parameter of a DbFunction has a null TypeMapping - i.e. the current provider cannot map that parameter's CLR type to a SQL parameter type. A DbFunction call must translate every argument to a provider parameter; an unmappable parameter type breaks translation, so the function registration is rejected at validation.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:406
{
throw new InvalidOperationException(
RelationalStrings.DbFunctionInvalidReturnEntityType(
dbFunction.ModelName, dbFunction.ReturnType.ShortDisplayName(), elementType.ShortDisplayName()));
}
if ((entityType.BaseType != null || entityType.GetDerivedTypes().Any())
&& entityType.FindDiscriminatorProperty() == null)
{
throw new InvalidOperationException(
RelationalStrings.TableValuedFunctionNonTph(dbFunction.ModelName, entityType.DisplayName()));
}
}
foreach (var parameter in dbFunction.Parameters)
{
if (parameter.TypeMapping == null)
{
throw new InvalidOperationException(
RelationalStrings.DbFunctionInvalidParameterType(
parameter.Name,
dbFunction.ModelName,
parameter.ClrType.ShortDisplayName()));
}
}
}
/// <summary>
/// Validates the function mapping for an entity type.
/// </summary>
/// <param name="entityType">The entity type to validate.</param>
/// <param name="logger">The logger to use.</param>
protected virtual void ValidateDbFunctionMapping(
IEntityType entityType,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
var mappedFunctionName = entityType.GetFunctionName();View on GitHub (pinned to dbf9771522)
Solutions
- Change the parameter type to a primitive the provider maps (int, string, DateTime, etc.).
- For enum/custom types, apply a value converter (HasConversion) so the parameter maps to a supported primitive.
- If the type is legitimately needed, add a provider-specific type mapping plugin.
Example fix
// before public List<Post> PostsByStatus(PostStatus status) => null!; // PostStatus enum unmapped modelBuilder.HasDbFunction(() => PostsByStatus(default)); // throws: parameter type unmapped // after - map enum to int modelBuilder.HasDbFunction(() => PostsByStatus(default)); // ensure the enum is converted: modelBuilder.Entity<Post>().Property(p => p.Status).HasConversion<int>(); // or change the DbFunction signature to take int directly: // public List<Post> PostsByStatus(int status) => null!;
Defensive patterns
Strategy: validation
Validate before calling
foreach (var fn in context.Model.GetDbFunctions())
{
foreach (var param in fn.Parameters)
{
if (param.TypeMapping == null)
{
// will throw - use a mapped primitive or add a value converter for this param type.
}
}
} Prevention
- Use provider-mappable primitive parameter types for DbFunctions.
- Add HasConversion for enum/custom parameter types.
- Validate all DbFunction parameters against the provider's type map at build time.
When it happens
Trigger: Registering a DbFunction with a parameter whose CLR type the provider does not map (custom struct/enum without conversion, or an unsupported primitive like char/decimal-precision edge cases on some providers). Thrown at model validation.
Common situations: Enum parameters without HasConversion; custom value-type parameters; provider-specific gaps (e.g. certain spatial/time types); porting DbFunctions between providers with narrower type coverage.
Related errors
- The DbFunction '{function}' has an invalid return type '{typ
- 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/f0e54553e5fe2261.
Report an issue: GitHub.