dotnet/efcore · error · InvalidOperationException

Couldn't find nested type '{name}' on containing type '{cont

Error message

Couldn't find nested type '{name}' on containing type '{containingType.Name}'

What it means

Thrown by GetClrType when containingType.GetNestedType(name) returns null. The CLR reflection lookup for a nested type (e.g. Outer.Inner) on its containing type failed - the nested type is not found via reflection on the resolved containing type. Part of EF Core's precompiled-query C#-to-LINQ translation.

Source

Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:1207

            {
                return typeof(Nullable<>);
            }

            var name = symbol.ContainingType is null
                ? typeSymbol.ToDisplayString(QualifiedTypeNameSymbolDisplayFormat)
                : typeSymbol.Name;

            if (symbol.IsGenericType)
            {
                name += '`' + symbol.Arity.ToString();
            }

            if (symbol.ContainingType is not null)
            {
                var containingType = ResolveType(symbol.ContainingType);

                return containingType.GetNestedType(name)
                    ?? throw new InvalidOperationException(
                        $"Couldn't find nested type '{name}' on containing type '{containingType.Name}'");
            }

            return GetClrTypeFromAssembly(typeSymbol.ContainingAssembly, name);
        }

        Type GetClrTypeFromAssembly(IAssemblySymbol? assemblySymbol, string name)
            => (assemblySymbol is null
                    ? Type.GetType(name)!
                    : Type.GetType($"{name}, {assemblySymbol.Name}"))
                // If we can't find the Type, check the assembly where the user's DbContext type lives; this is primarily to support
                // testing, where user code is in an assembly that's built as part of the the test and loaded into a specific
                // AssemblyLoadContext (which gets unloaded later).
                ?? _additionalAssembly?.GetType(name)
                ?? throw new InvalidOperationException(
                    $"Couldn't resolve CLR type '{name}' in assembly '{assemblySymbol?.Name}'");

        Dictionary<string[], Type> LoadAnonymousTypes(IAssemblySymbol assemblySymbol)

View on GitHub (pinned to dbf9771522)

Solutions

  1. Verify the nested type exists and is accessible via reflection in the loaded assembly version.
  2. Check for assembly version mismatches between compile-time and runtime.
  3. Ensure the nested type's visibility (public) allows reflection lookup.
Defensive patterns

Strategy: validation

Validate before calling

// Before translating, verify nested types are reflectable
var nested = containingType.GetNestedType(name,
    System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (nested is null)
    throw new InvalidOperationException(
        $"Nested type '{name}' not found on '{containingType.FullName}'.");

Prevention

When it happens

Trigger: Translating a reference to a nested type where GetNestedType returns null: private/internal nested types, nested generics with mismatched arity suffix, or types removed across versions.

Common situations: Non-public nested types; nested generic types where arity suffix computation differs; obfuscated/renamed types; assembly version mismatch where the nested type was removed or relocated.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/addc25382b68847d. Report an issue: GitHub.