{"record":{"id":"35129a82dfe9c63f","repo":"dotnet/efcore","slug":"couldn-t-resolve-clr-type-name-in-assembly-a","errorCode":null,"errorMessage":"Couldn't resolve CLR type '{name}' in assembly '{assemblySymbol?.Name}'","messagePattern":"Couldn't resolve CLR type '(.+?)' in assembly '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1222,"sourceCode":"                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)\n        {\n            Assembly? assembly;\n            try\n            {\n                assembly = Assembly.Load(assemblySymbol.Name);\n            }\n            catch (FileNotFoundException)\n            {\n                // If we can't find the assembly, use 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                assembly = _additionalAssembly\n                    ?? throw new InvalidOperationException($\"Could not load assembly for IAssemblySymbol '{assemblySymbol.Name}'\");\n            }\n","sourceCodeStart":1204,"sourceCodeEnd":1240,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1204-L1240","documentation":"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.).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the assembly containing the type via CSharpToLinqTranslator.Load(compilation, dbContext, additionalAssembly: typeof(TheType).Assembly).","Ensure the runtime has loaded the assembly before translation (AppDomain.CurrentDomain.AssemblyResolve handler, or pre-jit touch).","Match package/assembly versions between the Compilation and the runtime; add a binding redirect if the simple name matches but the version differs.","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."],"exampleFix":"// before\ntranslator.Load(compilation, dbContext);\n\n// after\ntranslator.Load(\n    compilation,\n    dbContext,\n    additionalAssembly: typeof(MyDomainType).Assembly);","handlingStrategy":"validation","validationCode":"// Confirm a CLR type is loadable before invoking the translator.\nbool IsTypeLoadable(string fullyQualifiedName, System.Reflection.Assembly? additional)\n    => Type.GetType(fullyQualifiedName) is not null\n       || (additional?.GetType(fullyQualifiedName) is not null);\n\n// For each referenced type symbol, e.g.:\nforeach (var typeName in requiredTypeNames)\n    if (!IsTypeLoadable(typeName, additionalAssembly))\n        throw new InvalidOperationException($\"Type '{typeName}' is not loadable; pass additionalAssembly or align versions.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    return translator.Translate(node, semanticModel);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Couldn't resolve CLR type\"))\n{\n    throw new InvalidOperationException(\n        \"A referenced CLR type could not be loaded at translation time. \" +\n        \"Pass its assembly via Load(compilation, ctx, additionalAssembly), or align package versions. \" +\n        \"Original: \" + ex.Message, ex);\n}","preventionTips":["Always pass additionalAssembly pointing at the assembly containing the user's query code when running the translator out-of-process.","Match package versions between the Compilation and the runtime; add binding redirects if necessary.","Wire AppDomain.CurrentDomain.AssemblyResolve to the user's assembly catalog when hosting the translator."],"tags":["ef-core","query-translation","assembly-loading","reflection","type-resolution","precompiled-queries"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}