dotnet/efcore · error · InvalidOperationException

ObjectCreation: couldn't find initialized member '{lValueSym

Error message

ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e}

What it means

The initializer's property/field symbol resolved via Roslyn, but the reflection lookup (GetProperty/GetField) on the constructed type returned null. The member is visible to the compiler but not accessible via reflection, so the translator cannot bind it.

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 dbf9771522)

Solutions

  1. Make the initialized member public
  2. Add [InternalsVisibleTo] for the consuming assembly
  3. Initialize only reflection-accessible members inside precompiled queries

Example fix

// before
internal string Code { get; set; }
// usage: new Item { Code = "x" }
// after
public string Code { get; set; }
Defensive patterns

Strategy: validation

Validate before calling

// Confirm initialized members are reflection-accessible.
var pi = typeof(Item).GetProperty("Code", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (pi is null) { /* make public / add InternalsVisibleTo */ }

Prevention

When it happens

Trigger: Initializing an internal, private, or explicit-interface property/field that is not reflection-accessible from the consuming assembly, or whose assembly version differs at runtime.

Common situations: Setting internal properties on shared entity DTOs across projects; initializing members defined only in a mismatched assembly version.

Related errors


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