dotnet/efcore · error · InvalidOperationException

Could not load assembly for IAssemblySymbol '{assemblySymbol

Error message

Could not load assembly for IAssemblySymbol '{assemblySymbol.Name}'

What it means

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.

Source

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

                // 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}'");
            }

            // Get all the anonymous type in the assembly, and index them by the ordered names of their properties.
            // Note that anonymous types are generic, so we don't have property types in the key.

            // TODO: An alternative strategy would be to just generate the types as we need them (with ref.emit) - that's probably safer.
            // TODO: Though it may mean that the resulting CLR Type can't be anonymous (Type.IsAnonymousType()) - not sure that matters.
            return assembly.GetTypes()
                .Where(t => t.IsAnonymousType())
                .ToDictionary(t => t.GetProperties().Select(x => x.Name).ToArray(), t => t, new ArrayStructuralComparer<string>());
        }
    }

    private sealed class ArrayStructuralComparer<T> : IEqualityComparer<T[]>
    {
        public bool Equals(T[]? x, T[]? y)
            => x is null ? y is null : y is not null && x.SequenceEqual(y);

View on GitHub (pinned to dbf9771522)

Solutions

  1. Pass the user assembly via the additionalAssembly parameter of CSharpToLinqTranslator.Load.
  2. Ensure the user assembly is loaded into the process before translation occurs.
Defensive patterns

Strategy: validation

Validate before calling

// If the query uses anonymous types, ensure the defining assembly is available
try { Assembly.Load(assemblyName); }
catch (FileNotFoundException) when (additionalAssembly is null)
{
    throw new InvalidOperationException(
        $"Assembly '{assemblyName}' (anonymous types) cannot be loaded; pass additionalAssembly.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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