dotnet/efcore · error · InvalidOperationException

Couldn't resolve CLR type '{name}' in assembly '{assemblySym

Error message

Couldn't resolve CLR type '{name}' in assembly '{assemblySymbol?.Name}'

What it means

Thrown by GetClrTypeFromAssembly when neither Type.GetType('name, assembly') nor _additionalAssembly?.GetType(name) can resolve the type. The fully-qualified type name could not be found in the referenced assembly or the additional-assembly fallback. Part of EF Core's precompiled-query C#-to-LINQ translation.

Source

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

                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)
        {
            Assembly? assembly;
            try
            {
                assembly = Assembly.Load(assemblySymbol.Name);
            }
            catch (FileNotFoundException)
            {
                // If we can't find the assembly, use 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).
                assembly = _additionalAssembly
                    ?? throw new InvalidOperationException($"Could not load assembly for IAssemblySymbol '{assemblySymbol.Name}'");
            }

View on GitHub (pinned to dbf9771522)

Solutions

  1. Ensure the assembly containing the type is loaded, or pass it via the additionalAssembly parameter of CSharpToLinqTranslator.Load.
  2. Confirm the type's full name matches across versions.
  3. Disable trimming for that assembly if using NativeAOT/trimming.
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check that every referenced type is resolvable at runtime
var resolved = Type.GetType($"{fullTypeName}, {assemblyName}") ?? additionalAssembly?.GetType(fullTypeName);
if (resolved is null)
    throw new InvalidOperationException(
        $"Type '{fullTypeName}' cannot be loaded; add the assembly or pass additionalAssembly.");

Prevention

When it happens

Trigger: Translating any type reference where the assembly is not loaded in the current AppLoadContext, or the type name does not match (renamed/moved namespace).

Common situations: Assembly not loaded at runtime; type moved between namespaces across versions; trimming/NativeAOT removing the type; AssemblyLoadContext isolation (especially in tests).

Related errors


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