dotnet/efcore · error · InvalidOperationException

Couldn't resolve CLR type

Error message

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

What it means

Thrown by CSharpToLinqTranslator.GetClrTypeFromAssembly when neither Type.GetType("{name}, {assemblySymbol.Name}") nor the fallback _additionalAssembly.GetType(name) could resolve the CLR type for the given ITypeSymbol. The translator needs the concrete CLR Type to build reflection-based expressions (MemberInfo, MethodInfo, etc.).

Solutions

  1. Pass the assembly containing the type via CSharpToLinqTranslator.Load(compilation, dbContext, additionalAssembly: typeof(TheType).Assembly).
  2. Ensure the runtime has loaded the assembly before translation (AppDomain.CurrentDomain.AssemblyResolve handler, or pre-jit touch).
  3. Match package/assembly versions between the Compilation and the runtime; add a binding redirect if the simple name matches but the version differs.
  4. If the type lives in a collectible ALC, keep the ALC alive for the duration of translation, or move the type into the default ALC.

Example fix

// before
translator.Load(compilation, dbContext);

// after
translator.Load(
    compilation,
    dbContext,
    additionalAssembly: typeof(MyDomainType).Assembly);
Defensive patterns

Strategy: validation

Validate before calling

// Confirm a CLR type is loadable before invoking the translator.
bool IsTypeLoadable(string fullyQualifiedName, System.Reflection.Assembly? additional)
    => Type.GetType(fullyQualifiedName) is not null
       || (additional?.GetType(fullyQualifiedName) is not null);

// For each referenced type symbol, e.g.:
foreach (var typeName in requiredTypeNames)
    if (!IsTypeLoadable(typeName, additionalAssembly))
        throw new InvalidOperationException($"Type '{typeName}' is not loadable; pass additionalAssembly or align versions.");

Try / catch

try
{
    return translator.Translate(node, semanticModel);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Couldn't resolve CLR type"))
{
    throw new InvalidOperationException(
        "A referenced CLR type could not be loaded at translation time. " +
        "Pass its assembly via Load(compilation, ctx, additionalAssembly), or align package versions. " +
        "Original: " + ex.Message, ex);
}

Prevention

When it happens

Trigger: A query references a type whose assembly is referenced by the Roslyn Compilation but is not actually loaded into the runtime context where translation runs; or the type's fully-qualified name differs (namespace rename, version skew); or the type lives in a test assembly loaded only into a collectible AssemblyLoadContext that has been unloaded.

Common situations: Precompiled-query/source-generator host running in a context where referenced assemblies are not yet loaded; EF Core test fixtures with AssemblyLoadContext; package version mismatch where the symbol's ContainingAssembly.Name differs from the loaded assembly identity; namespace/type renames after an upgrade.

Related errors


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

Appendix: 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 3a2006ef56)