dotnet/efcore · error · InvalidOperationException
ObjectCreation: couldn't find initialized member
Error message
ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e} What it means
Thrown when an object initializer assigns to a property or field whose Roslyn symbol was resolved, but the corresponding reflection lookup (GetProperty or GetField) returned null. The translator found the lValue symbol (IPropertySymbol or IFieldSymbol) but could not locate the actual MemberInfo on the resolved Type via reflection. This mirrors the [208] pattern for constructor object initializers specifically.
Solutions
- Verify assembly version consistency — ensure the same package version is used at compile time and precompilation time.
- If the property is an explicit interface implementation, initialize it through the interface or restructure.
- For init-only properties, ensure the declaring assembly is the correct version that exposes the init setter.
- If the property is from an external type, pass that assembly via additionalAssembly.
- Move object construction outside the precompiled query and capture the constructed object as a variable.
Example fix
// before — property not found on loaded type
var query = ctx.Users.Select(u => new UserDto { Email = u.Email }); // Email is init-only from older package
// after — ensure correct package version, or use constructor
var query = ctx.Users.Select(u => new UserDto(u.Email)); Defensive patterns
Strategy: validation
Validate before calling
// Before precompiling, verify initialized members are findable via reflection
foreach (var objCreation in querySyntaxTree.DescendantNodes().OfType<ObjectCreationExpressionSyntax>())
{
if (objCreation.Initializer?.Expressions is not [AssignmentExpressionSyntax, ..]) continue;
var type = semanticModel.GetSymbolInfo(objCreation).Symbol is IMethodSymbol ctor
? ResolveTypeForCheck(ctor.ContainingType) : null;
if (type is null) continue;
foreach (var init in objCreation.Initializer.Expressions.OfType<AssignmentExpressionSyntax>())
{
var lValueSymbol = semanticModel.GetSymbolInfo(init.Left).Symbol;
MemberInfo? mi = lValueSymbol switch
{
IPropertySymbol p => type.GetProperty(p.Name),
IFieldSymbol f => type.GetField(f.Name),
_ => null
};
if (mi is null && lValueSymbol is IPropertySymbol or IFieldSymbol)
ReportError($"Initialized member '{lValueSymbol.Name}' not found via reflection");
}
} Prevention
- Keep package versions consistent to avoid member metadata mismatches.
- Avoid explicit interface implementation properties in initializers.
- For init-only properties, ensure the declaring assembly is at the correct version.
- Move object construction outside the precompiled query if member resolution is uncertain.
- Prefer constructor parameters over property initializers for types from external assemblies.
When it happens
Trigger: A precompiled query creates an object and initializes a property/field that exists in the Roslyn compilation but is not found on the loaded CLR type via reflection. Causes: explicit interface implementation properties whose metadata name differs, version skew renaming/removing the member, the member is an init-only setter not exposed via GetProperty default lookup, or the containing type is loaded from a different assembly.
Common situations: Initializing properties on types from assemblies with version mismatches; initializing explicit interface implementation properties; initializing init-only properties (C# 9+) where reflection's GetProperty may not find the setter; properties shadowed with `new` keyword where the reflection lookup finds the wrong declaration; init-only setters introduced in a package update not reflected in the loaded assembly.
Related errors
- ObjectCreation: unsupported initializer for member of type
- Couldn't find single Add method on type
- ObjectCreation: Missing constructor
- ObjectCreation: non-assignment initializer expression of…
- Invocation: couldn't find method
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/2389b0cc0e8d3884.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:869
{
if (e is not AssignmentExpressionSyntax { Left: var lValue, Right: var value })
{
throw new NotSupportedException(
$"ObjectCreation: non-assignment initializer expression of type '{e.GetType().Name}': {objectCreation}");
}
var lValueSymbol = _semanticModel.GetSymbolInfo(lValue).Symbol;
var memberInfo = lValueSymbol switch
{
IPropertySymbol p => (MemberInfo?)type.GetProperty(p.Name),
IFieldSymbol f => type.GetField(f.Name),
_ => throw new InvalidOperationException(
$"ObjectCreation: unsupported initializer for member of type '{lValueSymbol?.GetType().Name}': {e}")
};
return memberInfo is null
? throw new InvalidOperationException(
$"ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e}")
: Bind(memberInfo, Visit(value));
}));
// Non-assignment initializer => list initializer (new List<int> { 1, 2, 3 })
default:
// Find the correct Add() method on the collection type
// TODO: This doesn't work if there are multiple Add() methods (contrived). Complete solution would be to find the base
// TODO: type for all initializer expressions and find an Add overload of that type (or a superclass thereof)
var addMethod = type.GetMethods().SingleOrDefault(m => m.Name == "Add" && m.GetParameters().Length == 1);
if (addMethod is null)
{
throw new InvalidOperationException(
$"Couldn't find single Add method on type '{type.Name}', required for list initializer");
}
// TODO: Dictionary initializer, where each ElementInit has more than one expression
View on GitHub (pinned to 3a2006ef56)