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

In an object initializer assignment, the left-hand side resolved to a symbol that is neither an IPropertySymbol nor an IFieldSymbol. The translator can only Bind properties and fields, so any other member kind (event, indexer, method group) is unsupported.

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

Solutions

  1. Initialize only properties and fields in the object initializer
  2. Set unsupported members (events, indexers) after construction, outside the query

Example fix

// before
var s = new Service { PropertyChanged = handler };
// after
var s = new Service();
s.PropertyChanged += handler;
Defensive patterns

Strategy: validation

Validate before calling

// Ensure initializer assignments target only properties or fields.
foreach (var m in typeof(Service).GetMembers())
    if (m is not (PropertyInfo or FieldInfo)) { /* do not init via initializer */ }

Prevention

When it happens

Trigger: An initializer assignment to an event (obj.MyEvent += handler form via initializer), an indexer, or another non-property/non-field member inside a precompiled query.

Common situations: Attempting to wire up an event or set an indexer via object initializer syntax within a precompiled query expression.

Related errors


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