dotnet/efcore · error · InvalidOperationException

The entity type '{entityType}' is mapped to the 'DbFunction'

Error message

The entity type '{entityType}' is mapped to the 'DbFunction' named '{functionName}', but is derived from '{baseEntityType}'. Derived entity types cannot be mapped to a function.

What it means

Thrown when a derived entity type (one with a non-null BaseType) is mapped to a DbFunction. A DbFunction represents the entire result set for a type, so only a base/root type may be mapped; derived types inherit the function mapping from their base. Mapping a derived type in an inheritance hierarchy is therefore rejected.

Source

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

        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(),
                    entityType.ClrType.ShortDisplayName()));
        }

        if (mappedFunction.Parameters.Count > 0)
        {
            var parameters = "{"

View on GitHub (pinned to dbf9771522)

Solutions

  1. Remove the ToFunction/DbFunction mapping from the derived type and map the base/root type instead.
  2. Restructure so the DbFunction returns the base entity type and the hierarchy maps through it.

Example fix

// before
modelBuilder.Entity<DerivedBlog>().ToFunction("GetDerivedBlogs");

// after
modelBuilder.Entity<BaseBlog>().ToFunction("GetBlogs");
// DerivedBlog inherits the mapping
Defensive patterns

Strategy: validation

Validate before calling

foreach (var et in modelBuilder.Model.GetEntityTypes())
{
    if (et.GetFunctionName() != null && et.BaseType != null)
        throw new InvalidOperationException($"Derived type {et.DisplayName()} cannot map to a DbFunction");
}

Try / catch

try { _ = ctx.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains("Derived entity types cannot be mapped to a function")) { /* fix the derived-type ToFunction mapping */ }

Prevention

When it happens

Trigger: Calling ToFunction on a derived entity in a TPH/TPT/TPC hierarchy; configuring HasDbFunction mapping on a subclass that already has a base type.

Common situations: Trying to give each derived type its own function-backed query source; misunderstanding that DbFunction mapping is inherited, not per-type.

Related errors


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