dotnet/efcore · error · InvalidOperationException

The specified entity type

Error message

The specified entity type '{derivedType}' is not derived from '{entityType}'.

What it means

UpdateEntityType throws InvalidDerivedTypeInEntityProjection when the supplied 'derived type' does not have the projection's current StructuralType in its base-type chain. The provider was asked to narrow a projection to a derived entity type, but the inheritance relationship does not hold. StructuralType must already be an IEntityType (otherwise an UnreachableException is raised).

Solutions

  1. Ensure the type passed to UpdateEntityType is a genuine subtype of the current StructuralType.
  2. Verify your inheritance hierarchy is configured (HasBaseType / TPH configuration).
  3. Confirm OfType<T>() is called against a set whose base type is an ancestor of T.

Example fix

// before - Animal is not derived from Vehicle
var q = db.Set<Vehicle>().OfType<Animal>();

// after - use a real derived type
var q = db.Set<Vehicle>().OfType<Car>();
Defensive patterns

Strategy: validation

Validate before calling

static bool IsDerivedFrom(IEntityType baseType, IEntityType derived)
    => derived.GetAllBaseTypes().Contains(baseType);

// before calling UpdateEntityType
if (!IsDerivedFrom(projection.StructuralType as IEntityType, derivedType))
    throw new InvalidOperationException($"{derivedType.Name} is not derived from {projection.StructuralType.DisplayName()}");

Type guard

static bool IsValidDerivedType(IEntityType current, IEntityType derived)
    => derived.GetAllBaseTypes().Contains(current);

Prevention

When it happens

Trigger: OfType<T>() or inheritance materialization where the requested type is not actually a subtype of the projection's current entity type.

Common situations: Calling OfType<T>() with a type outside the queried entity's hierarchy; misconfigured inheritance hierarchies; passing an unrelated entity type to a projection-narrowing API.

Related errors


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

Appendix: 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 3a2006ef56)