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}', but no DbFunction with that name was found in the model. Ensure that the entity type mapping is configured using the model name of a function in the model.

What it means

Thrown in ValidateDbFunctionMapping when an entity type has a function mapping (entityType.GetFunctionName() returns a value) but the model contains no DbFunction with that model-name. FindDbFunction(mappedFunctionName) at RelationalModelValidator.cs:430-435 returns null. The mapping was set (e.g., via .ToFunction(name)) without a matching HasDbFunction registration.

Solutions

  1. Register the DbFunction with the exact same model-name used in ToFunction: modelBuilder.HasDbFunction(() => Foo()).HasName("dbo.MyTvf").
  2. Verify the spelling and schema qualifier (dbo. prefix) match exactly between ToFunction and HasDbFunction/HasName.
  3. Ensure the HasDbFunction call is on the same model being finalized — check conditional registration branches and IModelCacheKeyFactory.
  4. If you no longer intend the TVF mapping, remove the .ToFunction(name) call.

Example fix

// before
modelBuilder.Entity<Customer>()
    .ToFunction("dbo.ActiveCustomers");
// no matching HasDbFunction registered

// after
public IQueryable<Customer> ActiveCustomers()
    => FromExpression(() => ActiveCustomers());

modelBuilder.HasDbFunction(() => ActiveCustomers());
modelBuilder.Entity<Customer>().ToFunction("ActiveCustomers");
Defensive patterns

Strategy: validation

Validate before calling

// Cross-check every entity's ToFunction name resolves to a registered DbFunction.
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
    var fnName = et.GetFunctionName();
    if (fnName is null) continue;
    if (et.Model.FindDbFunction(fnName) is null)
        throw new InvalidOperationException($"Entity '{et.Name}' is mapped to function '{fnName}' which is not registered via HasDbFunction.");
}

Try / catch

try { _ = ctx.Model; }
catch (InvalidOperationException ex) when (ex.Message.Contains("no DbFunction with that name was found"))
{
    logger.LogError("ToFunction name has no matching HasDbFunction. {Msg}", ex.Message);
    throw;
}

Prevention

When it happens

Trigger: Calling modelBuilder.Entity<T>().ToFunction("dbo.MyTvf") without a corresponding modelBuilder.HasDbFunction(() => MyMethod(...)). Or a typo between the name passed to ToFunction and the DbFunction's configured Name. Also when a DbFunction is conditionally registered (e.g., in a provider-specific branch that didn't run).

Common situations: Renaming a TVF in ToFunction but forgetting to rename the HasDbFunction; copy-paste errors; refactoring IModelConfiguration that dropped the HasDbFunction call; multi-tenant/shared model where the DbFunction registration is data-driven and was skipped.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/a97602a71f08bd22. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:433

    /// <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();
        if (mappedFunctionName == null)
        {
            return;
        }

        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(),

View on GitHub (pinned to 3a2006ef56)