{"record":{"id":"bb62095bd2de6054","repo":"dotnet/efcore","slug":"queryrootdifferententitytype","errorCode":"QueryRootDifferentEntityType","errorMessage":"The replacement entity type: {entityType} does not have same name and CLR type as entity type this query root represents.","messagePattern":"The replacement entity type: (.+?) does not have same name and CLR type as entity type this query root represents\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/Internal/TableValuedFunctionQueryRootExpression.cs","lineNumber":78,"sourceCode":"            arguments.Add(newArgument);\n            changed |= argument != newArgument;\n        }\n\n        return changed\n            ? new TableValuedFunctionQueryRootExpression(EntityType, Function, arguments)\n            : this;\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 override EntityQueryRootExpression UpdateEntityType(IEntityType entityType)\n        => entityType.ClrType != EntityType.ClrType\n            || entityType.Name != EntityType.Name\n                ? throw new InvalidOperationException(CoreStrings.QueryRootDifferentEntityType(entityType.DisplayName()))\n                : new TableValuedFunctionQueryRootExpression(entityType, Function, Arguments);\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    protected override void Print(ExpressionPrinter expressionPrinter)\n    {\n        expressionPrinter.Append(Function.Name);\n        expressionPrinter.Append(\"(\");\n        expressionPrinter.VisitCollection(Arguments);\n        expressionPrinter.Append(\")\");\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/TableValuedFunctionQueryRootExpression.cs#L60-L96","documentation":"Thrown by TableValuedFunctionQueryRootExpression.UpdateEntityType (TableValuedFunctionQueryRootExpression.cs:78) when the replacement entity type differs from the query root's entity type in either CLR type or name. A table-valued-function query root is bound to a specific entity type; replacing it with an incompatible one would desync the TVF mapping from the shaper, so EF refuses.","triggerScenarios":"Calling UpdateEntityType on a TableValuedFunctionQueryRootExpression with an IEntityType whose ClrType or Name differs from the original. Encountered by EF internals (e.g. model conformance / replacing query roots during precompilation) or by advanced code that manipulates query trees / implements a custom IQueryTranslationPreprocessor.","commonSituations":"Custom query-root rewriting that swaps entity types; using an entity type from a different model instance; TPT/TPC or entity-splitting scenarios where a TVF query root is rebound to a derived/different type; bugs in providers/extensions that call UpdateEntityType with mismatched types.","solutions":["Ensure the entity type passed to UpdateEntityType has the same ClrType and Name as the original query root's EntityType.","Recreate the query root for the new entity type instead of calling UpdateEntityType with a mismatched type.","If rebinding to a derived type, build a new TableValuedFunctionQueryRootExpression for that type.","Verify you are using entity types from the same IModel the query was compiled against."],"exampleFix":"// before - rebinding a TVF query root to an incompatible entity type\nvar newRoot = tvfQueryRoot.UpdateEntityType(otherEntityType); // ClrType/Name differ -> throws\n\n// after - ensure the replacement matches, or construct a fresh root for the target type\nvar matchingType = model.FindEntityType(originalEntityType.Name)!; // same name + CLR type\nvar newRoot = tvfQueryRoot.UpdateEntityType(matchingType);\n// or: var fresh = new TableValuedFunctionQueryRootExpression(targetType, function, args);","handlingStrategy":"validation","validationCode":"// Before UpdateEntityType, ensure name + CLR type match.\nstatic bool IsCompatible(IEntityType original, IEntityType replacement)\n    => original.ClrType == replacement.ClrType && original.Name == replacement.Name;\n// Usage: if (!IsCompatible(root.EntityType, newType)) { /* build a fresh root */ }","typeGuard":"static bool EntityTypeMatches(IEntityType a, IEntityType b) => a.Name == b.Name && a.ClrType == b.ClrType;","tryCatchPattern":"try { var r = root.UpdateEntityType(newType); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have same name and CLR type\")) {\n    // Replacement entity type is incompatible; construct a new query root instead.\n    var fresh = new TableValuedFunctionQueryRootExpression(newType, function, args);\n}","preventionTips":["Only pass entity types with matching name + CLR type to UpdateEntityType.","Construct a fresh query root when rebinding to a different type.","Use entity types from the same IModel the query was compiled against.","Add a guard asserting name/CLR-type equality before rebinding."],"tags":["ef-core","query-root","tvf","entity-type","provider"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}