dotnet/efcore · error · InvalidOperationException

Encountered unknown identifier name '{identifierName}', whic

Error message

Encountered unknown identifier name '{identifierName}', which doesn't correspond to a lambda parameter or captured variable

What it means

At the end of `VisitIdentifierName`, after excluding lambda parameters, the user's `DbContext`, and symbols that Roslyn's data-flow analysis reports as flowing in (`_dataFlowsIn`), the translator throws `InvalidOperationException`. The identifier refers to something it cannot model: not a parameter, not the context, and not a captured local.

Source

Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:458

        }

        // The Translate entry point into the translator uses Roslyn's data flow analysis to locate all local variables flowing in
        // (e.g. captured variables), and populates the _dataFlowsIn dictionary with them (with null values).
        if (symbol is ILocalSymbol localSymbol && _dataFlowsIn.TryGetValue(localSymbol, out var memberExpression))
        {
            // The first time we see a flowing-in variable, we create MemberExpression for it and cache it in _dataFlowsIn.
            return memberExpression
                ?? (_dataFlowsIn[localSymbol] =
                    Field(
                        Constant(new FakeClosureFrameClass()),
                        new FakeFieldInfo(
                            typeof(FakeClosureFrameClass),
                            ResolveType(localSymbol.Type),
                            localSymbol.Name,
                            localSymbol.NullableAnnotation is NullableAnnotation.NotAnnotated)));
        }

        throw new InvalidOperationException(
            $"Encountered unknown identifier name '{identifierName}', which doesn't correspond to a lambda parameter or captured variable");
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override Expression VisitImplicitArrayCreationExpression(ImplicitArrayCreationExpressionSyntax implicitArrayCreation)
    {
        if (_semanticModel.GetTypeInfo(implicitArrayCreation).Type is not IArrayTypeSymbol arrayTypeSymbol)
        {
            throw new InvalidOperationException($"ArrayCreation: non-array type symbol: {implicitArrayCreation}");
        }

        if (arrayTypeSymbol.Rank > 1)
        {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Capture the external value into a local that clearly flows into the query so data-flow analysis picks it up.
  2. Replace static/external references with a captured local variable assigned before the query.
  3. If it is a type reference, use `typeof(T)` instead of the bare name.

Example fix

// before
var q = ctx.Items.Where(i => i.Code == Constants.Code);   // static, not captured
// after
var code = Constants.Code;
var q = ctx.Items.Where(i => i.Code == code);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every non-parameter identifier is either the DbContext or a captured local flowing in.
var dataFlowsIn = semanticModel.AnalyzeDataFlow(node).DataFlowsIn;
var offenders = node.DescendantNodes().OfType<IdentifierNameSyntax>()
    .Where(id =>
    {
        if (semanticModel.GetSymbolInfo(id).Symbol is not { } s) return true; // separate (217) error
        if (s is not (ILocalSymbol or IFieldSymbol or IPropertySymbol)) return false;
        var t = semanticModel.GetTypeInfo(id).Type;
        if (t is not null && t.Name.Contains("DbSet")) return true; // separate (218) error
        return s is ILocalSymbol local && !dataFlowsIn.Contains(local);
    })
    .ToList();
if (offenders.Count != 0)
    throw new NotSupportedException(
        "Query references identifiers that are neither parameters, the DbContext, nor captured locals flowing in.");

translator.Translate(node, semanticModel);

Try / catch

try { translator.Translate(node, semanticModel); }
catch (InvalidOperationException ex) when (ex.Message.Contains("doesn't correspond to a lambda parameter or captured variable"))
{ /* capture the external value into a local and rebuild */ }

Prevention

When it happens

Trigger: A precompiled query referencing an identifier that is a static member, a method group, a constant from an unreferenced scope, or any symbol Roslyn's `AnalyzeDataFlow(...).DataFlowsIn` did not include.

Common situations: Static properties/constants, namespace/type references used as values, or captures that the data-flow analysis excluded (e.g. conditionally initialized locals) appearing inside the translated query.

Related errors


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