{"record":{"id":"2389b0cc0e8d3884","repo":"dotnet/efcore","slug":"objectcreation-couldn-t-find-initialized-member","errorCode":null,"errorMessage":"ObjectCreation: couldn't find initialized member '{lValueSymbol.Name}': {e}","messagePattern":"ObjectCreation: couldn't find initialized member '(.+?)': (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":869,"sourceCode":"                    {\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(\n                        $\"Couldn't find single Add method on type '{type.Name}', required for list initializer\");\n                }\n\n                // TODO: Dictionary initializer, where each ElementInit has more than one expression\n","sourceCodeStart":851,"sourceCodeEnd":887,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L851-L887","documentation":"The initializer's property/field symbol resolved via Roslyn, but the reflection lookup (GetProperty/GetField) on the constructed type returned null. The member is visible to the compiler but not accessible via reflection, so the translator cannot bind it.","triggerScenarios":"Initializing an internal, private, or explicit-interface property/field that is not reflection-accessible from the consuming assembly, or whose assembly version differs at runtime.","commonSituations":"Setting internal properties on shared entity DTOs across projects; initializing members defined only in a mismatched assembly version.","solutions":["Make the initialized member public","Add [InternalsVisibleTo] for the consuming assembly","Initialize only reflection-accessible members inside precompiled queries"],"exampleFix":"// before\ninternal string Code { get; set; }\n// usage: new Item { Code = \"x\" }\n// after\npublic string Code { get; set; }","handlingStrategy":"validation","validationCode":"// Confirm initialized members are reflection-accessible.\nvar pi = typeof(Item).GetProperty(\"Code\", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);\nif (pi is null) { /* make public / add InternalsVisibleTo */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make initialized members public, or grant InternalsVisibleTo","Initialize only reflection-accessible members inside precompiled queries","Keep assembly versions consistent between build and runtime"],"tags":["efcore","precompiled-queries","reflection","object-initializers","csharp"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}