{"record":{"id":"4f62b5ac49e90351","repo":"dotnet/efcore","slug":"the-replacement-entity-type-entitytype-does-not","errorCode":null,"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/Internal/TableValuedFunctionQueryRootExpression.cs#L60-L96","documentation":"Thrown by TableValuedFunctionQueryRootExpression.UpdateEntityType when the replacement entity type's CLR type or full name differs from the entity type the query root was created for. UpdateEntityType is meant for replacing an entity type with an equivalent one (e.g., a different IEntityType instance for the same entity), not for switching to a genuinely different entity.","triggerScenarios":"Internal EF Core pipeline calls UpdateEntityType during query processing (e.g., for TPT/TPC inheritance mapping or entity type replacement) and passes an IEntityType whose ClrType or Name doesn't match. This is typically an internal invariant violation, not a user API call.","commonSituations":"Using table-valued functions (HasDbFunction with IsBuiltIn or TVF mapping) combined with inheritance hierarchies (TPT/TPC) where entity type replacement logic encounters a mismatch. An EF Core internal bug in the query pipeline's entity type rewriting pass. Rare: a custom IEntityTypeSource or model configuration that produces conflicting entity type definitions.","solutions":["Verify your table-valued function mapping and entity type configuration are consistent — the TVF should return rows for exactly one entity type.","If using inheritance + TVF, ensure the base and derived types share the same CLR type name as expected by the function mapping.","Check for duplicate entity type registrations with different CLR types that could collide.","File an issue at https://github.com/dotnet/efcore with the model configuration and TVF setup if this appears to be an internal bug."],"exampleFix":"// before — TVF mapped to a type that gets replaced with a different CLR type during query\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(GetOrders))!)\n    .HasName(\"dbo.GetOrders\");\n// inheritance causes UpdateEntityType to see a mismatched ClrType\n\n// after — ensure TVF entity type is consistent with inheritance\nmodelBuilder.Entity<Order>().ToTable(\"Orders\");\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(GetOrders))!)\n    .HasName(\"dbo.GetOrders\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    var result = await context.Set<Order>().FromSqlInterpolated($\"SELECT * FROM dbo.GetOrders()\").ToListAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have same name and CLR type\"))\n{\n    logger.LogError(ex, \"TVF query root entity type mismatch — check model configuration\");\n    // Simplify: avoid inheritance overrides on TVF-mapped entities\n    throw;\n}","preventionTips":["Keep table-valued function mappings on simple entity types without complex inheritance hierarchies.","Ensure IEntityType configurations are consistent — no duplicate registrations with different CLR types.","Test TVF queries in integration tests to catch configuration mismatches early.","Report internal mismatches to the EF Core team with a minimal model + TVF repro."],"tags":["tvf","query-root","entity-type","inheritance","internal-error"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}