dotnet/efcore · error · NotSupportedException

DebugInfo nodes are not supporting when translating expressi

Error message

DebugInfo nodes are not supporting when translating expression trees to C#

What it means

VisitDebugInfo throws NotSupportedException because DebugInfoExpression nodes (JIT sequence-point / debug-info markers) carry no information that maps to C# source. The translator is used by EF Core's precompiled-query and runtime-model source generators to render LINQ expression trees as C#, so a node type with no source equivalent is rejected outright.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:1196

    /// </summary>
    protected virtual ExpressionSyntax GenerateUnknownValue(object value)
    {
        var type = value.GetType();
        return type.IsValueType
            && value.Equals(type.GetDefaultValue())
                ? DefaultExpression(Generate(type))
                : value is IRelationalQuotableExpression relationalQuotableExpression
                && Translate(relationalQuotableExpression.Quote()) is ExpressionSyntax expressionSyntax
                    ? expressionSyntax
                    : throw new NotSupportedException(
                        $"Encountered a constant of unsupported type '{value.GetType().Name}'. Only primitive constant nodes are supported."
                        + Environment.NewLine
                        + value);
    }

    /// <inheritdoc />
    protected override Expression VisitDebugInfo(DebugInfoExpression node)
        => throw new NotSupportedException("DebugInfo nodes are not supporting when translating expression trees to C#");

    /// <inheritdoc />
    protected override Expression VisitDefault(DefaultExpression node)
    {
        Result = DefaultExpression(Generate(node.Type));

        return node;
    }

    /// <inheritdoc />
    protected override Expression VisitGoto(GotoExpression gotoNode)
    {
        Result = GotoStatement(SyntaxKind.GotoStatement, TranslateLabelTarget(gotoNode.Target));
        return gotoNode;
    }

    /// <inheritdoc />
    protected override Expression VisitInvocation(InvocationExpression invocation)

View on GitHub (pinned to dbf9771522)

Solutions

  1. Strip DebugInfoExpression nodes from the tree before precompilation using an ExpressionVisitor that overrides VisitDebugInfo to return the node without re-emitting it (or skips it from parent block enumerations).
  2. Stop inserting Expression.DebugInfo(...) into lambdas consumed by EF Core precompiled queries.
  3. If the node originates from a third-party extension, report it — EF Core's translator should never see debug info for compiler-generated query lambdas.

Example fix

// before (tree builder inserts debug info)
var body = Expression.Block(
    Expression.DebugInfo(Document, 0, 0, 0, 10),
    originalBody);
// after (strip debug-info nodes before handing to EF Core)
public class DebugInfoStripper : ExpressionVisitor {
    protected override Expression VisitDebugInfo(DebugInfoExpression node) => node; // dropped from block assembly
}
Defensive patterns

Strategy: validation

Validate before calling

// Run before precompiling: assert no DebugInfo nodes exist
public sealed class DebugInfoDetector : ExpressionVisitor {
    public bool Found;
    protected override Expression VisitDebugInfo(DebugInfoExpression node) { Found = true; return node; }
}
var d = new DebugInfoDetector(); d.Visit(lambda.Body);
if (d.Found) throw new InvalidOperationException("Tree contains DebugInfo nodes; strip them first.");

Prevention

When it happens

Trigger: An expression tree fed to the translator (a precompiled query lambda or a model-building lambda) contains a DebugInfoExpression. The C# compiler never emits these for normal user lambdas; they appear when an ExpressionVisitor/rewriter or a manual Expression.DebugInfo(...) call inserts one, or a third-party library emits debug-info nodes into a tree EF Core then tries to precompile.

Common situations: Custom expression-tree rewriting middleware that annotates trees with DebugInfo; programmatically constructed query trees passed through EF Core's precompiled-query source generator; integrating a library that instruments expression trees.

Related errors


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