{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L1220-L1256","documentation":"Thrown by LoadAnonymousTypes when Assembly.Load(assemblySymbol.Name) throws FileNotFoundException AND _additionalAssembly is null. The assembly containing anonymous type definitions (needed to resolve 'new { ... }' projections) cannot be loaded. Part of EF Core's precompiled-query C#-to-LINQ translation.","triggerScenarios":"Translating a query that uses anonymous types (new { ... }) where the user's assembly is not loaded into the translator's AppLoadContext and no additionalAssembly was provided.","commonSituations":"Test scenarios where user code lives in a separate AssemblyLoadContext that has been unloaded; design-time hosting where the user assembly is not loaded into the translating process.","solutions":["Pass the user assembly via the additionalAssembly parameter of CSharpToLinqTranslator.Load.","Ensure the user assembly is loaded into the process before translation occurs."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// If the query uses anonymous types, ensure the defining assembly is available\ntry { Assembly.Load(assemblyName); }\ncatch (FileNotFoundException) when (additionalAssembly is null)\n{\n    throw new InvalidOperationException(\n        $\"Assembly '{assemblyName}' (anonymous types) cannot be loaded; pass additionalAssembly.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the user assembly through additionalAssembly when translating queries with anonymous projections.","In test hosts, keep the user AssemblyLoadContext alive for the duration of translation."],"tags":["efcore","anonymous-types","assembly-loading","design-time"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}