{"record":{"id":"8fedf4678ccdea4f","repo":"dotnet/efcore","slug":"objectcreation-unsupported-initializer-for-member","errorCode":null,"errorMessage":"ObjectCreation: unsupported initializer for member of type '{lValueSymbol?.GetType().Name}': {e}","messagePattern":"ObjectCreation: unsupported initializer for member of type '(.+?)': (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":864,"sourceCode":"            // 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                    }));\n\n            // Non-assignment initializer => list initializer (new List<int> { 1, 2, 3 })\n            default:\n                // Find the correct Add() method on the collection type\n                // TODO: This doesn't work if there are multiple Add() methods (contrived). Complete solution would be to find the base\n                // TODO: type for all initializer expressions and find an Add overload of that type (or a superclass thereof)\n                var addMethod = type.GetMethods().SingleOrDefault(m => m.Name == \"Add\" && m.GetParameters().Length == 1);\n                if (addMethod is null)\n                {\n                    throw new InvalidOperationException(","sourceCodeStart":846,"sourceCodeEnd":882,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L846-L882","documentation":"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.","triggerScenarios":"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.","commonSituations":"Attempting to wire up an event or set an indexer via object initializer syntax within a precompiled query expression.","solutions":["Initialize only properties and fields in the object initializer","Set unsupported members (events, indexers) after construction, outside the query"],"exampleFix":"// before\nvar s = new Service { PropertyChanged = handler };\n// after\nvar s = new Service();\ns.PropertyChanged += handler;","handlingStrategy":"validation","validationCode":"// Ensure initializer assignments target only properties or fields.\nforeach (var m in typeof(Service).GetMembers())\n    if (m is not (PropertyInfo or FieldInfo)) { /* do not init via initializer */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize only properties and fields via object initializer syntax in queries","Wire up events and set indexers after construction, outside the query"],"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"}