dotnet/efcore · error · NotSupportedException

ObjectCreation: non-assignment initializer expression of typ

Error message

ObjectCreation: non-assignment initializer expression of type '{e.GetType().Name}': {objectCreation}

What it means

VisitObjectCreationExpression throws NotSupportedException inside an assignment-style object initializer (new X { A = 1 }) when one of the initializer elements is not an AssignmentExpressionSyntax. The translator builds a MemberInit only from a uniform list of assignments.

Source

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

            : parameterTypes.Length == 0 // For structs, there's no actual parameterless constructor
                ? New(type)
                : throw new InvalidOperationException($"ObjectCreation: Missing constructor: {objectCreation}");

        switch (objectCreation.Initializer)
        {
            // No initializers, just return the NewExpression
            case null or { Expressions: [] }:
                return newExpression;

            // 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));
                    }));

View on GitHub (pinned to dbf9771522)

Solutions

  1. Keep initializers uniform: either all assignments or all collection elements, not both in one initializer
  2. Initialize the nested collection in a separate statement after construction

Example fix

// before
var b = new Blog { Name = "x", Posts = { new Post() } };
// after
var b = new Blog { Name = "x" };
b.Posts.Add(new Post());
Defensive patterns

Strategy: validation

Validate before calling

// Ensure object initializers are uniform: all assignments or all collection elements, never mixed.
// Review each 'new X { ... }' in the query source for mixed initializer kinds.

Prevention

When it happens

Trigger: An object initializer whose first element is an assignment (entering the assignment branch) but a later element is a non-assignment (e.g. an embedded collection Add or a method call mixed into the same initializer).

Common situations: Mixing property assignments and collection-add expressions within one object initializer; pasting collection initializer elements into an assignment initializer.

Related errors


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