{"record":{"id":"0b89c549a1b69188","repo":"dotnet/efcore","slug":"the-specified-entity-type-derivedtype-is-not-d","errorCode":null,"errorMessage":"The specified entity type '{derivedType}' is not derived from '{entityType}'.","messagePattern":"The specified entity type '(.+?)' is not derived from '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/Expressions/StructuralTypeProjectionExpression.cs","lineNumber":293,"sourceCode":"            return BindComplexProperty(complex, clientEval);\n        }\n\n        // Entity member not found\n        propertyBase = null;\n        return null;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual StructuralTypeProjectionExpression UpdateEntityType(IEntityType derivedType)\n        => StructuralType is not IEntityType entityType\n            ? throw new UnreachableException($\"{nameof(UpdateEntityType)} called on non-entity type '{StructuralType.DisplayName()}'\")\n            : !derivedType.GetAllBaseTypes().Contains(StructuralType)\n                ? throw new InvalidOperationException(\n                    CosmosStrings.InvalidDerivedTypeInEntityProjection(\n                        derivedType.DisplayName(), StructuralType.DisplayName()))\n                : new StructuralTypeProjectionExpression(Object, derivedType);\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)\n        => expressionPrinter.Visit(Object);\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/Expressions/StructuralTypeProjectionExpression.cs#L275-L311","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar q = ctx.Set<Animal>().OfType<Machine>(); // Machine doesn't derive from Animal\n\n// after\nvar q = ctx.Set<Animal>().OfType<Dog>(); // Dog derives from Animal","handlingStrategy":"validation","validationCode":"static bool IsDerivedFrom(IEntityType baseType, IEntityType derivedType)\n    => derivedType.GetAllBaseTypes().Contains(baseType);","typeGuard":"static bool IsProperDerivedType(IEntityType baseType, IEntityType derivedType)\n    => !Equals(baseType, derivedType) && derivedType.GetAllBaseTypes().Contains(baseType);","tryCatchPattern":"try { var q = ctx.Set<Animal>().OfType<Dog>().ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"is not derived from\"))\n{ /* pass a type that actually derives from the projection's entity */ }","preventionTips":["Verify inheritance hierarchy mapping before using OfType<T>().","Resolve derived types from the model metadata rather than hardcoding."],"tags":["cosmos","inheritance","projection","derived-type"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}