dotnet/efcore · error · InvalidOperationException
The element type of the result of '{dbFunction}' is mapped t
Error message
The element type of the result of '{dbFunction}' is mapped to '{entityType}'. This is not supported since '{entityType}' is part of hierarchy but does not contain a discriminator property. Only TPH hierarchies can be mapped to a TVF. What it means
ValidateDbFunction (RelationalModelValidator.cs:394-399) throws for a non-scalar DbFunction (TVF) whose returned entity type is part of an inheritance hierarchy but has no discriminator property. A TVF result is a flat row set; EF can only route those rows to hierarchy members via a discriminator (TPH). TPT/TPC hierarchies or hierarchies lacking a discriminator cannot be the row type of a TVF, so the function is rejected.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:397
|| ((IConventionModel)model).IsOwned(elementType)
|| (entityType == null && model.GetEntityTypes().Any(e => e.ClrType == elementType)))
{
throw new InvalidOperationException(
RelationalStrings.DbFunctionInvalidIQueryableOwnedReturnType(
dbFunction.ModelName, elementType.ShortDisplayName()));
}
if (entityType == null)
{
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>View on GitHub (pinned to dbf9771522)
Solutions
- Switch the hierarchy to TPH with a configured discriminator (modelBuilder.Entity<Base>().HasDiscriminator(...)) so the TVF rows can be classified.
- Return a flat non-hierarchical entity (keyless DTO) from the TVF and map hierarchy members separately.
- Remove the DbFunction mapping if the hierarchy cannot use TPH and map the query another way.
Example fix
// before - TPT/TPC hierarchy as TVF row type
public IQueryable<Employee> EmployeesInDept(int deptId) => null!;
// Person -> Employee inheritance is TPT (no discriminator)
modelBuilder.HasDbFunction(() => EmployeesInDept(default)); // throws
// after - TPH with discriminator supports the TVF
modelBuilder.Entity<Person>().HasDiscriminator(p => p.Type).HasValue<Employee>("E");
modelBuilder.HasDbFunction(() => EmployeesInDept(default)); Defensive patterns
Strategy: validation
Validate before calling
foreach (var fn in context.Model.GetDbFunctions())
{
if (!fn.IsScalar)
{
var elementType = fn.ReturnType.GetGenericArguments()[0];
var et = context.Model.FindEntityType(elementType);
if (et != null
&& (et.BaseType != null || et.GetDerivedTypes().Any())
&& et.FindDiscriminatorProperty() == null)
{
// will throw - use TPH with a discriminator or a non-hierarchical row type.
}
}
} Prevention
- Only map TVFs to TPH hierarchies that have a discriminator.
- Return a flat keyless entity from TVFs when inheritance isn't TPH.
- Re-audit DbFunctions when changing inheritance strategy.
When it happens
Trigger: Mapping a DbFunction returning IQueryable<T> where T is in a TPT/TPC hierarchy, or a TPH hierarchy whose discriminator was removed/misconfigured. Thrown at model validation.
Common situations: TPT/TPC inheritance combined with TVF mapping; hierarchy discriminator removed while a TVF still targets the hierarchy; refactoring inheritance strategy without updating DbFunctions.
Related errors
- 'HasDiscriminatorInJsonId' or 'HasRootDiscriminatorInJsonId'
- The entity type '{entityType}' is mapped to a SQL query, but
- The DbFunction '{function}' has an invalid return type '{typ
- The DbFunction '{function}' returns '{type}', but '{elementT
- The entity type '{entityType}' is mapped to the 'DbFunction'
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/8b4c7347a7ce1ea8.
Report an issue: GitHub.