{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L836-L872","documentation":"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.","triggerScenarios":"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).","commonSituations":"Mixing property assignments and collection-add expressions within one object initializer; pasting collection initializer elements into an assignment initializer.","solutions":["Keep initializers uniform: either all assignments or all collection elements, not both in one initializer","Initialize the nested collection in a separate statement after construction"],"exampleFix":"// before\nvar b = new Blog { Name = \"x\", Posts = { new Post() } };\n// after\nvar b = new Blog { Name = \"x\" };\nb.Posts.Add(new Post());","handlingStrategy":"validation","validationCode":"// Ensure object initializers are uniform: all assignments or all collection elements, never mixed.\n// Review each 'new X { ... }' in the query source for mixed initializer kinds.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never mix property assignments and collection-add elements in one object initializer","Initialize nested collections in separate statements after construction"],"tags":["efcore","precompiled-queries","object-initializers","csharp"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}