dotnet/efcore · error · InvalidOperationException
Could not find symbol for typeof() expression
Error message
Could not find symbol for typeof() expression: {typeOf} What it means
Thrown by VisitTypeOfExpression when Roslyn's GetSymbolInfo on the type inside a typeof() expression returns a null or non-ITypeSymbol. The translator needs the type symbol to resolve the CLR Type and emit a Constant expression containing that Type. Without the symbol, it cannot determine what type typeof() refers to and throws InvalidOperationException.
Solutions
- Ensure the type referenced in typeof() exists and its assembly is referenced by the project and available to the precompilation pipeline.
- Pass external assemblies containing the referenced type via additionalAssembly to CSharpToLinqTranslator.Load().
- If using typeof() for generic method type arguments, try using the generic method syntax directly (Set<Blog>() instead of Set(typeof(Blog))) so the type is resolved through the generic parameter path.
- Verify the project compiles cleanly before precompilation — unresolved typeof types usually stem from upstream compile errors.
- If the type is defined in the same solution, ensure dependent projects are built first.
Example fix
// before — typeof with unresolvable type
var query = ctx.Set(Type.GetType("MyApp.Entities.Blog, MyApp")).Cast<object>();
// after — use generic syntax with directly referenced type
var query = ctx.Set<Blog>(); Defensive patterns
Strategy: validation
Validate before calling
// Before precompiling, verify typeof() expressions resolve to type symbols
var unresolvedTypeOfs = querySyntaxTree.DescendantNodes()
.OfType<TypeOfExpressionSyntax>()
.Where(t => semanticModel.GetSymbolInfo(t.Type).Symbol is not ITypeSymbol);
if (unresolvedTypeOfs.Any())
ReportError("Unresolved typeof() expression in precompiled query — check type references"); Prevention
- Ensure types used in typeof() are from assemblies available to the precompilation pipeline.
- Prefer generic method syntax (Set<Blog>()) over typeof-based dispatch (Set(typeof(Blog))).
- Pass external assemblies via additionalAssembly to Load().
- Ensure all projects in the solution are built before running precompilation.
- Avoid typeof() with Type.GetType() string-based resolution inside queries.
When it happens
Trigger: A precompiled query contains a typeof() expression whose type argument Roslyn cannot resolve. Causes: the type doesn't exist in the compilation (missing reference or typo), the type is an open generic parameter used incorrectly, the type is defined in an assembly not referenced by the compilation, or the semantic model is stale.
Common situations: Using typeof() to pass type arguments to generic methods inside precompiled queries (e.g. ctx.Set(typeof(Blog)) or reflection-based dispatch); referencing types from assemblies not loaded by the precompilation pipeline; using typeof() with types from not-yet-compiled projects in the same solution; stale semantic models after significant code changes.
Related errors
- Could not find symbol for method invocation
- MemberAccess: Couldn't find symbol for member
- ObjectCreation: couldn't find IMethodSymbol for constructor
- ArrayCreation: multi-dimensional array
- Couldn't find single Add method on type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/2e5909bc7aa91d67.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)