dotnet/efcore · error · InvalidOperationException
The specified entity type '{derivedType}' is not derived fro
Error message
The specified entity type '{derivedType}' is not derived from '{entityType}'. What it means
StructuralTypeProjectionExpression.UpdateEntityType(derivedType) narrows the projected entity type to a derived type. It requires that the current StructuralType appears in derivedType's base-types chain (i.e. derivedType genuinely derives from it). Passing a non-derived type throws InvalidOperationException.
Source
Thrown at src/EFCore.Cosmos/Query/Internal/Expressions/StructuralTypeProjectionExpression.cs:293
return BindComplexProperty(complex, clientEval);
}
// Entity member not found
propertyBase = null;
return null;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual StructuralTypeProjectionExpression UpdateEntityType(IEntityType derivedType)
=> StructuralType is not IEntityType entityType
? throw new UnreachableException($"{nameof(UpdateEntityType)} called on non-entity type '{StructuralType.DisplayName()}'")
: !derivedType.GetAllBaseTypes().Contains(StructuralType)
? throw new InvalidOperationException(
CosmosStrings.InvalidDerivedTypeInEntityProjection(
derivedType.DisplayName(), StructuralType.DisplayName()))
: new StructuralTypeProjectionExpression(Object, derivedType);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
=> expressionPrinter.Visit(Object);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.View on GitHub (pinned to dbf9771522)
Solutions
- Pass a type that derives from the projection's entity type (verify with entityType.GetAllBaseTypes()).
- Ensure your inheritance mapping (base/derived) matches the OfType/cast you use in queries.
- If the projection should be a different root, build it from the correct entity set rather than narrowing the wrong projection.
Example fix
// before var q = ctx.Set<Animal>().OfType<Machine>(); // Machine doesn't derive from Animal // after var q = ctx.Set<Animal>().OfType<Dog>(); // Dog derives from Animal
Defensive patterns
Strategy: validation
Validate before calling
static bool IsDerivedFrom(IEntityType baseType, IEntityType derivedType)
=> derivedType.GetAllBaseTypes().Contains(baseType); Type guard
static bool IsProperDerivedType(IEntityType baseType, IEntityType derivedType)
=> !Equals(baseType, derivedType) && derivedType.GetAllBaseTypes().Contains(baseType); Try / catch
try { var q = ctx.Set<Animal>().OfType<Dog>().ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("is not derived from"))
{ /* pass a type that actually derives from the projection's entity */ } Prevention
- Verify inheritance hierarchy mapping before using OfType<T>().
- Resolve derived types from the model metadata rather than hardcoding.
When it happens
Trigger: Calling UpdateEntityType with a type that is not a subtype of the projection's entity type — e.g. OfType<UnrelatedType>() or casting to a sibling type in the hierarchy.
Common situations: TPH inheritance where OfType is used with a type outside the hierarchy; refactoring hierarchies so a previously-derived type no longer derives; provider bugs during derived-type projection.
Related errors
- An Azure Cosmos DB container name is defined on entity type
- Cosmos automatic-indexing configuration was set on entity ty
- A partition key is defined on entity type '{entityType}', wh
- 'HasDiscriminatorInJsonId' or 'HasRootDiscriminatorInJsonId'
- 'HasShadowId' was called on a non-root entity type '{entityT
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/0b89c549a1b69188.
Report an issue: GitHub.