dotnet/efcore · error · NotSupportedException

ObjectCreation: non-assignment initializer expression of…

Error message

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

What it means

Thrown as NotSupportedException when an object creation initializer with the assignment-pattern (detected because the first expression is an AssignmentExpressionSyntax) contains a non-assignment expression among its initializers. The translator assumes that once the first initializer is an assignment (like `new Blog { Name = "x" }`), all initializers in the same brace block should be assignments. Encountering a non-assignment expression in that context throws.

Solutions

  1. Rewrite the object creation to use a consistent initializer style — either all assignment initializers (for object properties) or all element expressions (for collections), never mixed.
  2. If using C# 12+ collection expressions, use the older explicit collection-initializer syntax that the translator recognizes: `new List<int> { 1, 2, 3 }` instead of `[1, 2, 3]`.
  3. If the initializer was produced by a source generator or refactoring tool, inspect the generated code and fix the initializer manually.
  4. Split the creation into a `new` expression followed by separate assignment statements outside the query.

Example fix

// before — mixed or unusual initializer
var query = ctx.Blogs.Select(b => new Blog { Name = b.Name, b.Url }); // second is not assignment

// after — consistent assignment initializers
var query = ctx.Blogs.Select(b => new Blog { Name = b.Name, Url = b.Url });
Defensive patterns

Strategy: validation

Validate before calling

// Before precompiling, check for inconsistent initializer expressions
foreach (var objCreation in querySyntaxTree.DescendantNodes().OfType<ObjectCreationExpressionSyntax>())
{
    if (objCreation.Initializer?.Expressions is [AssignmentExpressionSyntax, .. var rest])
    {
        var nonAssignments = rest.Where(e => e is not AssignmentExpressionSyntax);
        if (nonAssignments.Any())
            ReportError("Mixed initializer expressions in object creation — use consistent assignment style");
    }
}

Prevention

When it happens

Trigger: A precompiled query creates an object with a mixed or malformed initializer block where assignment-style initializers (Name = value) are interspersed with non-assignment expressions, or where the Roslyn classification of the first initializer as an AssignmentExpressionSyntax is followed by a different expression type in subsequent initializers. This is syntactically unusual in valid C# but can occur with collection-expression syntax mixing or compiler-generated code.

Common situations: Extremely rare in hand-written code. May occur with source-generated query code that produces unusual initializer syntax, or with C# collection expressions (`new List<int> { 1, 2, 3 }`) that Roslyn classifies differently across compiler versions. Also possible with IDE refactoring tools that produce mixed initializer patterns.

Related errors


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

Appendix: 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 3a2006ef56)