{"record":{"id":"addc25382b68847d","repo":"dotnet/efcore","slug":"couldn-t-find-nested-type-name-on-containing-t","errorCode":null,"errorMessage":"Couldn't find nested type '{name}' on containing type '{containingType.Name}'","messagePattern":"Couldn't find nested type '(.+?)' on containing type '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1207,"sourceCode":"            {\n                return typeof(Nullable<>);\n            }\n\n            var name = symbol.ContainingType is null\n                ? typeSymbol.ToDisplayString(QualifiedTypeNameSymbolDisplayFormat)\n                : typeSymbol.Name;\n\n            if (symbol.IsGenericType)\n            {\n                name += '`' + symbol.Arity.ToString();\n            }\n\n            if (symbol.ContainingType is not null)\n            {\n                var containingType = ResolveType(symbol.ContainingType);\n\n                return containingType.GetNestedType(name)\n                    ?? throw new InvalidOperationException(\n                        $\"Couldn't find nested type '{name}' on containing type '{containingType.Name}'\");\n            }\n\n            return GetClrTypeFromAssembly(typeSymbol.ContainingAssembly, name);\n        }\n\n        Type GetClrTypeFromAssembly(IAssemblySymbol? assemblySymbol, string name)\n            => (assemblySymbol is null\n                    ? Type.GetType(name)!\n                    : Type.GetType($\"{name}, {assemblySymbol.Name}\"))\n                // If we can't find the Type, check the assembly where the user's DbContext type lives; this is primarily to support\n                // testing, where user code is in an assembly that's built as part of the the test and loaded into a specific\n                // AssemblyLoadContext (which gets unloaded later).\n                ?? _additionalAssembly?.GetType(name)\n                ?? throw new InvalidOperationException(\n                    $\"Couldn't resolve CLR type '{name}' in assembly '{assemblySymbol?.Name}'\");\n\n        Dictionary<string[], Type> LoadAnonymousTypes(IAssemblySymbol assemblySymbol)","sourceCodeStart":1189,"sourceCodeEnd":1225,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1189-L1225","documentation":"Thrown by CSharpToLinqTranslator.GetClrType(INamedTypeSymbol) when the symbol has a ContainingType (i.e. it is a nested type) but Type.GetNestedType(name) on the resolved containing CLR type returned null. The name passed includes a '`arity' suffix when the nested type is generic, mirroring reflection's naming.","triggerScenarios":"A query references a nested type that is private/inaccessible to reflection (GetNestedType only returns public nested types by default with the plain string lookup), or a nested type whose arity suffix or name does not match what reflection expects, or a version of the containing assembly where the nested type no longer exists.","commonSituations":"Referencing private/internal nested types in a query lambda; mismatch between the assembly version the Compilation sees and the assembly actually loaded at runtime; renamed or removed nested type after a package upgrade.","solutions":["Avoid referencing private or non-public nested types in EF query lambdas; promote them to top-level or public if needed.","Align the assembly version used by the Roslyn Compilation with the one loaded at runtime (BindingRedirect / package version match).","If the nested type is generic, confirm its arity matches what reflection expects; consider closing the generic so the open-definition lookup is bypassed.","If the type was renamed/moved, update references in the query or pin the package version that still contains it."],"exampleFix":"// before — Outer.PrivateNested<T> referenced in a query, reflection can't see it\nsource.Where(x => x.Kind == Outer.PrivateNested<int>.KindValue);\n\n// after — make the nested type public, or reference only its public equivalent\nsource.Where(x => x.Kind == Outer.PublicNested.KindValue);","handlingStrategy":"validation","validationCode":"// Verify a nested type is reflectively discoverable before translation.\nbool IsNestedTypeAccessible(Type containing, string nestedName, int arity)\n{\n    var name = arity > 0 ? $\"{nestedName}`{arity}\" : nestedName;\n    return containing.GetNestedType(name,\n        System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic) is not null;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid referencing private or non-public nested types in EF query lambdas.","Keep the assembly version used by the Compilation aligned with the one loaded at runtime.","Prefer top-level types over nested types for anything referenced in queries."],"tags":["ef-core","query-translation","reflection","nested-types","type-resolution","roslyn"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}