dotnet/efcore · error · InvalidOperationException
Could not find symbol for anonymous object creation initiali
Error message
Could not find symbol for anonymous object creation initializer: {anonymousObjectCreation} What it means
In `VisitAnonymousObjectCreationExpression` the translator does `GetSymbolInfo(anonymousObjectCreation).Symbol` and expects an `IMethodSymbol` (the anonymous-type constructor). If Roslyn returns null it throws `InvalidOperationException(NoAnonymousSymbol + ...)`, meaning the semantic model could not bind the `new { ... }` expression to a constructor.
Source
Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:169
// TODO: Insert necessary Convert nodes etc. when the expected and actual types differ
return result;
}
/// <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 VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax anonymousObjectCreation)
{
// Creating an actual anonymous object means creating a new type, which can only be done with Reflection.Emit.
// At least for EF's purposes, it doesn't matter, so we build a placeholder.
if (_semanticModel.GetSymbolInfo(anonymousObjectCreation).Symbol is not IMethodSymbol constructorSymbol)
{
throw new InvalidOperationException(DesignStrings.NoAnonymousSymbol + " " + anonymousObjectCreation);
}
var anonymousType = ResolveType(constructorSymbol.ContainingType);
var parameters = constructorSymbol.Parameters.ToArray();
var parameterInfos = new ParameterInfo[parameters.Length];
var memberInfos = new MemberInfo[parameters.Length];
var arguments = new Expression[parameters.Length];
foreach (var initializer in anonymousObjectCreation.Initializers)
{
// If the initializer's name isn't explicitly specified, infer it from the initializer's expression like the compiler does
var name = initializer.NameEquals is not null
? initializer.NameEquals.Name.Identifier.Text
: initializer.Expression is MemberAccessExpressionSyntax memberAccess
? memberAccess.Name.Identifier.Text
: throw new InvalidOperationException(View on GitHub (pinned to dbf9771522)
Solutions
- Ensure the syntax tree is part of the same `Compilation` used to build the `SemanticModel` and passed to `Load`.
- Add all metadata references the query needs (entity types, BCL) to the compilation.
- If anonymous projection isn't required, project into a named DTO instead.
Example fix
// before var sm = otherCompilation.GetSemanticModel(tree); // wrong compilation translator.Translate(node, sm); // after var sm = compilation.GetSemanticModel(tree); translator.Translate(node, sm);
Defensive patterns
Strategy: validation
Validate before calling
// Confirm the SemanticModel binds the anonymous constructor before translating.
var symbol = semanticModel.GetSymbolInfo(anonymousNode).Symbol;
if (symbol is not IMethodSymbol)
throw new InvalidOperationException(
"The anonymous object creation could not be bound. " +
"Ensure the tree is in the same compilation and references are present.");
translator.Translate(anonymousNode, semanticModel); Try / catch
try { translator.Translate(node, semanticModel); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not find symbol for anonymous object creation"))
{ /* verify tree belongs to compilation, add references, retry */ } Prevention
- Build the `SemanticModel` from the same `Compilation` passed to `Load`.
- Include all references needed to bind anonymous types (entity + BCL assemblies).
- Prefer named DTO projections when binding is fragile.
When it happens
Trigger: Translating a precompiled query containing an anonymous-object creation (`new { a.X, b.Y }`) when the `SemanticModel` cannot resolve the anonymous-type constructor — typically because the compilation lacks references or the syntax tree was not added to the compilation that produced the semantic model.
Common situations: Building a `SemanticModel` from a `CSharpCompilation` that is missing references (so the anonymous type symbol is unbound), or passing a `SemanticModel` from a different compilation than the one given to `Load`.
Related errors
- AnonymousObjectCreation: unnamed initializer with non-Member
- Could not find type symbol for: {fullyQualifiedMetadataName}
- A compilation must be loaded.
- Could not resolve type symbol for: {parameter.Type}
- ArrayCreation: non-array type symbol: {arrayCreation}
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/37bf1be3aa57902c.
Report an issue: GitHub.