{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L846-L882","documentation":"Thrown as InvalidOperationException when the lValue (left side) of an object initializer assignment is not backed by an IPropertySymbol or IFieldSymbol. The translator only knows how to bind initializer assignments to properties and fields. If the lValue's symbol is something else — an event, a method group, an indexer, or null — it throws because it cannot determine what member to bind via reflection.","triggerScenarios":"A precompiled query creates an object with an initializer that assigns to a member that is not a property or field. Examples: assigning to an event (`new Pub { OnClick = handler }` where OnClick is an event), assigning to an indexer (unusual in initializers), or the lValue symbol being unresolved (null) because Roslyn couldn't determine what the assignment target is.","commonSituations":"Assigning to events in object initializers inside precompiled queries; using with-expressions or record primary constructors where the lValue maps to a parameter-backed property with unusual symbol metadata; assigning to properties backed by explicit interface implementations where the symbol type is unexpected; lValue symbol unresolved due to compile errors.","solutions":["Remove event assignments from object initializers in precompiled queries — subscribe to events outside the query body.","If assigning to a property that maps to a method or parameter symbol, restructure to assign to a standard auto-property or field instead.","Ensure the lValue in the initializer resolves cleanly — check that the property/field exists and is accessible.","If using records, prefer constructor parameters over init-only property assignments in precompiled queries.","Move object construction and property/event setup outside the precompiled query body."],"exampleFix":"// before — event in initializer\nvar query = ctx.Sensors.Select(s => new Monitor { Threshold = s.Max, OnAlert = handler });\n\n// after — separate event subscription\nvar query = ctx.Sensors.Select(s => new Monitor { Threshold = s.Max });\n// subscribe outside query","handlingStrategy":"validation","validationCode":"// Before precompiling, check that initializer lValues are properties or fields\nforeach (var objCreation in querySyntaxTree.DescendantNodes().OfType<ObjectCreationExpressionSyntax>())\n{\n    if (objCreation.Initializer?.Expressions is not [AssignmentExpressionSyntax, ..]) continue;\n    foreach (var init in objCreation.Initializer.Expressions)\n    {\n        if (init is AssignmentExpressionSyntax { Left: var lValue })\n        {\n            var symbol = semanticModel.GetSymbolInfo(lValue).Symbol;\n            if (symbol is not IPropertySymbol and not IFieldSymbol)\n                ReportError($\"Initializer target '{lValue}' is not a property or field\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not assign to events in object initializers inside precompiled queries.","Keep initializer assignments to simple auto-properties and public fields only.","For records, use constructor parameters rather than init-only property assignment.","Move complex object initialization (events, computed properties) outside the precompiled query body."],"tags":["precompiled-queries","roslyn","object-creation","initializer","member-binding","expression-tree"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}