dotnet/efcore · error · InvalidOperationException

Calling '{visitMethodName}' is not allowed. Visit the expres

Error message

Calling '{visitMethodName}' is not allowed. Visit the expression manually for the relevant part in the visitor.

What it means

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.

Source

Thrown at src/EFCore.Relational/Query/EnumerableExpression.cs:138

    public virtual EnumerableExpression AppendOrdering(OrderingExpression orderingExpression)
    {
        var orderings = Orderings.ToList();
        AppendOrdering(orderings, orderingExpression);

        return new EnumerableExpression(Selector, IsDistinct, Predicate, orderings);
    }

    private static void AppendOrdering(List<OrderingExpression> orderings, OrderingExpression orderingExpression)
    {
        if (!orderings.Any(o => o.Expression.Equals(orderingExpression.Expression)))
        {
            orderings.Add(orderingExpression.Update(orderingExpression.Expression));
        }
    }

    /// <inheritdoc />
    protected override Expression VisitChildren(ExpressionVisitor visitor)
        => throw new InvalidOperationException(
            CoreStrings.VisitIsNotAllowed($"{nameof(EnumerableExpression)}.{nameof(VisitChildren)}"));

    /// <inheritdoc />
    public override ExpressionType NodeType
        => ExpressionType.Extension;

    /// <inheritdoc />
    public override Type Type
        => typeof(IEnumerable<>).MakeGenericType(Selector.Type);

    /// <inheritdoc />
    public virtual void Print(ExpressionPrinter expressionPrinter)
    {
        expressionPrinter.AppendLine(nameof(EnumerableExpression) + ":");
        using (expressionPrinter.Indent())
        {
            expressionPrinter.Append("Selector: ");
            expressionPrinter.Visit(Selector);

View on GitHub (pinned to dbf9771522)

Solutions

  1. Override VisitExtension in your visitor and handle EnumerableExpression explicitly (visit Selector, Predicate, Orderings in the required order) without calling its VisitChildren.
  2. Avoid walking EF internal expression trees directly; use the public extension points (IMethodCallTranslator, IQueryTranslationPreprocessor) instead.
  3. Upgrade EF-intercepting libraries to versions compatible with your EF Core release.

Example fix

// before - generic visitor blows up
public override Expression Visit(Expression node)
{
    return base.Visit(node); // throws on EnumerableExpression
}

// after - handle the extension explicitly
protected override Expression VisitExtension(Expression node)
{
    if (node is EnumerableExpression ee)
    {
        var selector = Visit(ee.Selector);
        // rebuild manually; do NOT call ee.VisitChildren
        return ee.ApplySelector(selector);
    }
    return base.VisitExtension(node);
}
Defensive patterns

Strategy: validation

Validate before calling

static bool IsInternalEfExpression(Expression e)
    => e is EnumerableExpression
       || e.GetType().FullName?.StartsWith("Microsoft.EntityFrameworkCore.Query") == true;

if (visitorStack.Any(IsInternalEfExpression))
    throw new InvalidOperationException("Refusing to generically visit EF internal expressions; handle them explicitly.");

Type guard

bool IsEnumerableExpression(Expression e) => e is EnumerableExpression;

Try / catch

try { visitor.Visit(tree); }
catch (InvalidOperationException ex) when (ex.Message.Contains("is not allowed"))
{ /* handle EF internal node by overriding VisitExtension instead */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/5d7730573cf5a165. Report an issue: GitHub.