{"record":{"id":"1a98c7a3901b65c8","repo":"dotnet/efcore","slug":"objectcreation-non-assignment-initializer-express","errorCode":null,"errorMessage":"ObjectCreation: non-assignment initializer expression of type '{e.GetType().Name}': {objectCreation}","messagePattern":"ObjectCreation: non-assignment initializer expression of type '(.+?)': (.+?)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":854,"sourceCode":"            : parameterTypes.Length == 0 // For structs, there's no actual parameterless constructor\n                ? New(type)\n                : throw new InvalidOperationException($\"ObjectCreation: Missing constructor: {objectCreation}\");\n\n        switch (objectCreation.Initializer)\n        {\n            // No initializers, just return the NewExpression\n            case null or { Expressions: [] }:\n                return newExpression;\n\n            // Assignment initializer (new Blog { Name = \"foo\" })\n            case { Expressions: [AssignmentExpressionSyntax, ..] }:\n                return MemberInit(\n                    newExpression,\n                    objectCreation.Initializer.Expressions.Select(e =>\n                    {\n                        if (e is not AssignmentExpressionSyntax { Left: var lValue, Right: var value })\n                        {\n                            throw new NotSupportedException(\n                                $\"ObjectCreation: non-assignment initializer expression of type '{e.GetType().Name}': {objectCreation}\");\n                        }\n\n                        var lValueSymbol = _semanticModel.GetSymbolInfo(lValue).Symbol;\n                        var memberInfo = lValueSymbol switch\n                        {\n                            IPropertySymbol p => (MemberInfo?)type.GetProperty(p.Name),\n                            IFieldSymbol f => type.GetField(f.Name),\n\n                            _ => throw new InvalidOperationException(\n                                $\"ObjectCreation: unsupported initializer for member of type '{lValueSymbol?.GetType().Name}': {e}\")\n                        };\n\n                        return memberInfo is null\n                            ? throw new InvalidOperationException(\n                                $\"ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e}\")\n                            : Bind(memberInfo, Visit(value));\n                    }));","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L836-L872","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","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]`.","If the initializer was produced by a source generator or refactoring tool, inspect the generated code and fix the initializer manually.","Split the creation into a `new` expression followed by separate assignment statements outside the query."],"exampleFix":"// before — mixed or unusual initializer\nvar query = ctx.Blogs.Select(b => new Blog { Name = b.Name, b.Url }); // second is not assignment\n\n// after — consistent assignment initializers\nvar query = ctx.Blogs.Select(b => new Blog { Name = b.Name, Url = b.Url });","handlingStrategy":"validation","validationCode":"// Before precompiling, check for inconsistent initializer expressions\nforeach (var objCreation in querySyntaxTree.DescendantNodes().OfType<ObjectCreationExpressionSyntax>())\n{\n    if (objCreation.Initializer?.Expressions is [AssignmentExpressionSyntax, .. var rest])\n    {\n        var nonAssignments = rest.Where(e => e is not AssignmentExpressionSyntax);\n        if (nonAssignments.Any())\n            ReportError(\"Mixed initializer expressions in object creation — use consistent assignment style\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use consistent initializer style — all assignments or all element expressions, never mixed.","Avoid C# 12 collection expression syntax ([...]) inside precompiled queries; use traditional initializers.","Review auto-generated or refactored code for unusual initializer patterns before precompiling.","Split object creation into a constructor call plus separate assignments if initializers are complex."],"tags":["precompiled-queries","roslyn","object-creation","initializer","csharp-syntax","expression-tree"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}