{"record":{"id":"d4a7be082ad8039d","repo":"dotnet/efcore","slug":"could-not-load-assembly-for-iassemblysymbol-asse","errorCode":null,"errorMessage":"Could not load assembly for IAssemblySymbol '{assemblySymbol.Name}'","messagePattern":"Could not load assembly for IAssemblySymbol '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":1238,"sourceCode":"                // 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\n            // Get all the anonymous type in the assembly, and index them by the ordered names of their properties.\n            // Note that anonymous types are generic, so we don't have property types in the key.\n\n            // TODO: An alternative strategy would be to just generate the types as we need them (with ref.emit) - that's probably safer.\n            // TODO: Though it may mean that the resulting CLR Type can't be anonymous (Type.IsAnonymousType()) - not sure that matters.\n            return assembly.GetTypes()\n                .Where(t => t.IsAnonymousType())\n                .ToDictionary(t => t.GetProperties().Select(x => x.Name).ToArray(), t => t, new ArrayStructuralComparer<string>());\n        }\n    }\n\n    private sealed class ArrayStructuralComparer<T> : IEqualityComparer<T[]>\n    {\n        public bool Equals(T[]? x, T[]? y)\n            => x is null ? y is null : y is not null && x.SequenceEqual(y);\n","sourceCodeStart":1220,"sourceCodeEnd":1256,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1220-L1256","documentation":"Thrown by CSharpToLinqTranslator.LoadAnonymousTypes when Assembly.Load(assemblySymbol.Name) threw FileNotFoundException and the translator has no _additionalAssembly fallback. Anonymous-type definitions must be loaded reflectively so the translator can build the closed generic anonymous Type from its members; without either path it cannot continue.","triggerScenarios":"An EF query constructs an anonymous type (e.g. .Select(x => new { x.A, x.B })) and the assembly that contains the anonymous type definition is not loadable by simple name through Assembly.Load, with no additionalAssembly supplied as a fallback.","commonSituations":"Test harnesses where the user code lives in an assembly built and loaded into a specific AssemblyLoadContext that gets unloaded before the translator runs; source-generator host where the user assembly has not been loaded reflectively; an assembly whose simple name does not match its file identity.","solutions":["Pass the user's assembly (the one containing the anonymous type definition) as additionalAssembly in Load(...).","Keep the AssemblyLoadContext that holds the user assembly alive during translation (do not unload collectible ALCs before the precompiled-query pipeline finishes).","Avoid anonymous types in the translated query if the host cannot load the source assembly reflectively (project to a named DTO instead).","Ensure the assembly is discoverable by Assembly.Load (correct simple name, on the default load context, or wired through AppDomain.AssemblyResolve)."],"exampleFix":"// before\ntranslator.Load(compilation, dbContext);\n// query: db.Users.Select(u => new { u.Id, u.Name })\n\n// after\ntranslator.Load(\n    compilation,\n    dbContext,\n    additionalAssembly: userQueryAssembly);","handlingStrategy":"validation","validationCode":"// Verify the user assembly (anonymous types live there) is loadable or provided.\nbool CanLoadUserAssembly(Microsoft.CodeAnalysis.IAssemblySymbol asm, System.Reflection.Assembly? additional)\n{\n    try { System.Reflection.Assembly.Load(asm.Name); return true; }\n    catch (System.IO.FileNotFoundException) { return additional is not null; }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    translator.Load(compilation, dbContext, additionalAssembly: userAssembly);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not load assembly for IAssemblySymbol\"))\n{\n    throw new InvalidOperationException(\n        \"The user's assembly could not be loaded reflectively. \" +\n        \"Pass it as additionalAssembly and keep its AssemblyLoadContext alive.\", ex);\n}","preventionTips":["Always supply additionalAssembly when hosting the translator against user code.","Do not unload collectible AssemblyLoadContexts that hold user code until translation completes.","Prefer named DTOs over anonymous types when hosting the translator in environments where the user assembly may not be loadable."],"tags":["ef-core","query-translation","anonymous-types","assembly-loading","reflection","precompiled-queries"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}