{"record":{"id":"5d7730573cf5a165","repo":"dotnet/efcore","slug":"calling-visitmethodname-is-not-allowed-visit","errorCode":null,"errorMessage":"Calling '{visitMethodName}' is not allowed. Visit the expression manually for the relevant part in the visitor.","messagePattern":"Calling '(.+?)' is not allowed\\. Visit the expression manually for the relevant part in the visitor\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/EnumerableExpression.cs","lineNumber":138,"sourceCode":"    public virtual EnumerableExpression AppendOrdering(OrderingExpression orderingExpression)\n    {\n        var orderings = Orderings.ToList();\n        AppendOrdering(orderings, orderingExpression);\n\n        return new EnumerableExpression(Selector, IsDistinct, Predicate, orderings);\n    }\n\n    private static void AppendOrdering(List<OrderingExpression> orderings, OrderingExpression orderingExpression)\n    {\n        if (!orderings.Any(o => o.Expression.Equals(orderingExpression.Expression)))\n        {\n            orderings.Add(orderingExpression.Update(orderingExpression.Expression));\n        }\n    }\n\n    /// <inheritdoc />\n    protected override Expression VisitChildren(ExpressionVisitor visitor)\n        => throw new InvalidOperationException(\n            CoreStrings.VisitIsNotAllowed($\"{nameof(EnumerableExpression)}.{nameof(VisitChildren)}\"));\n\n    /// <inheritdoc />\n    public override ExpressionType NodeType\n        => ExpressionType.Extension;\n\n    /// <inheritdoc />\n    public override Type Type\n        => typeof(IEnumerable<>).MakeGenericType(Selector.Type);\n\n    /// <inheritdoc />\n    public virtual void Print(ExpressionPrinter expressionPrinter)\n    {\n        expressionPrinter.AppendLine(nameof(EnumerableExpression) + \":\");\n        using (expressionPrinter.Indent())\n        {\n            expressionPrinter.Append(\"Selector: \");\n            expressionPrinter.Visit(Selector);","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/EnumerableExpression.cs#L120-L156","documentation":"EnumerableExpression.VisitChildren deliberately throws InvalidOperationException to forbid generic visitor recursion. EnumerableExpression is an internal query expression whose Selector/Predicate/Orderings must be visited manually and in a specific order by the relational query pipeline; blindly calling the base visitor would break translation invariants.","triggerScenarios":"A custom ExpressionVisitor that calls visitor.Visit (or base.VisitChildren) on an EnumerableExpression node; reflecting over EF's expression tree and applying a generic visitor; a third-party EF extension that walks the tree without handling EnumerableExpression specially.","commonSituations":"Writing a custom query translator/visitor; debugging EF query trees; an EF extension library that does not special-case EnumerableExpression; an EF Core version upgrade where this node type was introduced and old visitors no longer handle it.","solutions":["Override VisitExtension in your visitor and handle EnumerableExpression explicitly (visit Selector, Predicate, Orderings in the required order) without calling its VisitChildren.","Avoid walking EF internal expression trees directly; use the public extension points (IMethodCallTranslator, IQueryTranslationPreprocessor) instead.","Upgrade EF-intercepting libraries to versions compatible with your EF Core release."],"exampleFix":"// before - generic visitor blows up\npublic override Expression Visit(Expression node)\n{\n    return base.Visit(node); // throws on EnumerableExpression\n}\n\n// after - handle the extension explicitly\nprotected override Expression VisitExtension(Expression node)\n{\n    if (node is EnumerableExpression ee)\n    {\n        var selector = Visit(ee.Selector);\n        // rebuild manually; do NOT call ee.VisitChildren\n        return ee.ApplySelector(selector);\n    }\n    return base.VisitExtension(node);\n}","handlingStrategy":"validation","validationCode":"static bool IsInternalEfExpression(Expression e)\n    => e is EnumerableExpression\n       || e.GetType().FullName?.StartsWith(\"Microsoft.EntityFrameworkCore.Query\") == true;\n\nif (visitorStack.Any(IsInternalEfExpression))\n    throw new InvalidOperationException(\"Refusing to generically visit EF internal expressions; handle them explicitly.\");","typeGuard":"bool IsEnumerableExpression(Expression e) => e is EnumerableExpression;","tryCatchPattern":"try { visitor.Visit(tree); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"is not allowed\"))\n{ /* handle EF internal node by overriding VisitExtension instead */ }","preventionTips":["Override VisitExtension and special-case EnumerableExpression instead of using base.Visit.","Prefer EF's public extension points over walking internal expression trees.","Keep EF-extension libraries version-aligned with the EF Core runtime."],"tags":["efcore","query","expression-tree","internal","visitor"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}