dotnet/efcore · error · InvalidOperationException

Could not find symbol for typeof() expression: {typeOf}

Error message

Could not find symbol for typeof() expression: {typeOf}

What it means

VisitTypeOfExpression throws InvalidOperationException when typeof(T) inside a precompiled query cannot be resolved to an ITypeSymbol by Roslyn. The translator needs the type symbol to map typeof to a Constant expression over System.Type.

Source

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

    ///     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 VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax lambda)
        => VisitLambdaExpression(lambda);

    /// <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 VisitTypeOfExpression(TypeOfExpressionSyntax typeOf)
    {
        if (_semanticModel.GetSymbolInfo(typeOf.Type).Symbol is not ITypeSymbol typeSymbol)
        {
            throw new InvalidOperationException(
                "Could not find symbol for typeof() expression: " + typeOf);
        }

        var type = ResolveType(typeSymbol);
        return Constant(type, typeof(Type));
    }

    /// <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 DefaultVisit(SyntaxNode node)
        => throw new NotSupportedException($"Unsupported syntax node of type '{node.GetType()}': {node}");

    private Expression VisitLambdaExpression(AnonymousFunctionExpressionSyntax lambda, Type? expectedType = null)
    {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Reference the assembly that defines the type used in typeof()
  2. Avoid typeof() of unresolved types in precompiled queries
  3. Capture the needed Type in a local variable outside the query and reference that

Example fix

// before (ExternalFlag in unreferenced assembly)
var q = ctx.Items.Where(i => i.Kind == typeof(ExternalFlag));
// after (referenced type)
var q = ctx.Items.Where(i => i.Kind == typeof(KnownFlag));
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the type used in typeof() is referenced by the precompilation.
var t = typeof(ExternalFlag);
// If the type is in another assembly, add a MetadataReference for that assembly.

Prevention

When it happens

Trigger: typeof(SomeType) where SomeType is in an unreferenced assembly, is an error type, or otherwise fails to bind in the precompilation.

Common situations: typeof() of a type defined in an assembly not referenced by the precompiled-query compilation; typeof of a type whose source has compile errors.

Related errors


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