dotnet/efcore · error · InvalidOperationException

ObjectCreation: unsupported initializer for member of type

Error message

ObjectCreation: unsupported initializer for member of type '{lValueSymbol?.GetType().Name}': {e}

What it means

Thrown as InvalidOperationException when the lValue (left side) of an object initializer assignment is not backed by an IPropertySymbol or IFieldSymbol. The translator only knows how to bind initializer assignments to properties and fields. If the lValue's symbol is something else — an event, a method group, an indexer, or null — it throws because it cannot determine what member to bind via reflection.

Solutions

  1. Remove event assignments from object initializers in precompiled queries — subscribe to events outside the query body.
  2. If assigning to a property that maps to a method or parameter symbol, restructure to assign to a standard auto-property or field instead.
  3. Ensure the lValue in the initializer resolves cleanly — check that the property/field exists and is accessible.
  4. If using records, prefer constructor parameters over init-only property assignments in precompiled queries.
  5. Move object construction and property/event setup outside the precompiled query body.

Example fix

// before — event in initializer
var query = ctx.Sensors.Select(s => new Monitor { Threshold = s.Max, OnAlert = handler });

// after — separate event subscription
var query = ctx.Sensors.Select(s => new Monitor { Threshold = s.Max });
// subscribe outside query
Defensive patterns

Strategy: validation

Validate before calling

// Before precompiling, check that initializer lValues are properties or fields
foreach (var objCreation in querySyntaxTree.DescendantNodes().OfType<ObjectCreationExpressionSyntax>())
{
    if (objCreation.Initializer?.Expressions is not [AssignmentExpressionSyntax, ..]) continue;
    foreach (var init in objCreation.Initializer.Expressions)
    {
        if (init is AssignmentExpressionSyntax { Left: var lValue })
        {
            var symbol = semanticModel.GetSymbolInfo(lValue).Symbol;
            if (symbol is not IPropertySymbol and not IFieldSymbol)
                ReportError($"Initializer target '{lValue}' is not a property or field");
        }
    }
}

Prevention

When it happens

Trigger: A precompiled query creates an object with an initializer that assigns to a member that is not a property or field. Examples: assigning to an event (`new Pub { OnClick = handler }` where OnClick is an event), assigning to an indexer (unusual in initializers), or the lValue symbol being unresolved (null) because Roslyn couldn't determine what the assignment target is.

Common situations: Assigning to events in object initializers inside precompiled queries; using with-expressions or record primary constructors where the lValue maps to a parameter-backed property with unusual symbol metadata; assigning to properties backed by explicit interface implementations where the symbol type is unexpected; lValue symbol unresolved due to compile errors.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/8fedf4678ccdea4f. Report an issue: GitHub.

Appendix: source

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

            // Assignment initializer (new Blog { Name = "foo" })
            case { Expressions: [AssignmentExpressionSyntax, ..] }:
                return MemberInit(
                    newExpression,
                    objectCreation.Initializer.Expressions.Select(e =>
                    {
                        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(

View on GitHub (pinned to 3a2006ef56)