{"record":{"id":"3bfe0fec0481b231","repo":"dotnet/efcore","slug":"the-replacement-entity-type-entitytype-does-not-3bfe0f","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/FromSqlQueryRootExpression.cs","lineNumber":81,"sourceCode":"    /// <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 Expression DetachQueryProvider()\n        => new FromSqlQueryRootExpression(EntityType, Sql, Argument);\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 FromSqlQueryRootExpression(entityType, Sql, Argument);\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 Expression VisitChildren(ExpressionVisitor visitor)\n    {\n        var argument = visitor.Visit(Argument);\n\n        return argument != Argument\n            ? new FromSqlQueryRootExpression(EntityType, Sql, argument)\n            : this;\n    }\n\n    /// <summary>","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/FromSqlQueryRootExpression.cs#L63-L99","documentation":"FromSqlQueryRootExpression.UpdateEntityType throws InvalidOperationException when the replacement IEntityType differs from the query root's entity type in either Name or CLR type. The query root is bound to a specific entity type (its SQL and argument are tied to it), so swapping to a different entity would be invalid; EF requires an exact name+CLR match.","triggerScenarios":"Calling UpdateEntityType with an IEntityType whose Name (model-qualified name) or ClrType differs from the original; a query-translation extension attempting to retarget a FromSql query root to a different entity; model remapping that changes the entity type mid-translation.","commonSituations":"Custom query translators/visitors that try to swap entity types; TPH/TPT inheritance remapping gone wrong; using a replacement entity type built for a different CLR type; EF extension libraries mismatching entity types during tree rewriting.","solutions":["Pass an IEntityType with the same Name and the same ClrType as the original query root's EntityType.","If you need a different entity, construct a new FromSqlQueryRootExpression for that entity rather than updating an existing one.","Avoid mutating query roots in custom visitors; rebuild the tree from the correct entity type."],"exampleFix":"// before\nvar newRoot = fromSqlRoot.UpdateEntityType(otherEntityType); // name/CLR differs -> throws\n\n// after - use a matching entity type, or build a new root\nvar matching = model.FindEntityType(typeof(Order))!; // same Name + ClrType\nvar newRoot = fromSqlRoot.UpdateEntityType(matching);\n// or:\nvar fresh = new FromSqlQueryRootExpression(queryProvider, otherEntityType, sql, arg);","handlingStrategy":"validation","validationCode":"static IEntityType ValidateSameEntity(IEntityType original, IEntityType replacement)\n{\n    if (replacement.Name != original.Name || replacement.ClrType != original.ClrType)\n        throw new InvalidOperationException($\"Replacement entity {replacement.DisplayName()} differs from {original.DisplayName()}.\");\n    return replacement;\n}","typeGuard":"static bool IsSameEntityType(IEntityType a, IEntityType b) => a.Name == b.Name && a.ClrType == b.ClrType;","tryCatchPattern":"try { return root.UpdateEntityType(replacement); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have same name and CLR type\"))\n{ /* construct a fresh query root for the new entity instead */ throw; }","preventionTips":["Only pass an entity type with identical Name and ClrType to UpdateEntityType.","Build a new FromSqlQueryRootExpression when you need a different entity.","Do not rewrite query roots mid-translation in custom visitors."],"tags":["efcore","query","expression-tree","internal","entity-type"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}