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

  1. Change the parameter type to a primitive the provider maps (int, string, DateTime, etc.).
  2. For enum/custom types, apply a value converter (HasConversion) so the parameter maps to a supported primitive.
  3. 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

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


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/f0e54553e5fe2261. Report an issue: GitHub.