dotnet/efcore · error · OperationException

The exception ' ' was thrown while attempting to find…

Error message

The exception '{rootException}' was thrown while attempting to find 'DbContext' types. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

What it means

OperationException when scanning the assembly for DbContext types itself threw an exception (other than an already-wrapped OperationException). The original exception message is wrapped; this indicates discovery logic, not context construction, failed.

Solutions

  1. Read the wrapped inner exception to find the failing type/assembly.
  2. Ensure all referenced assemblies are present (run `dotnet restore`/publish dependencies).
  3. Reduce the startup assembly surface, or move contexts to a dedicated assembly so discovery walks fewer types.
  4. Catch and guard problematic static initializers so they do not fault during reflection.

Example fix

// before - static initializer throws during type scan
public class PluginModule { static PluginModule() { File.ReadAllText("missing"); } }

// after - lazy/guarded initialization
public class PluginModule { static PluginModule() { } public void Init() { ... } }
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity-scan the startup assembly for load issues before invoking the tools
foreach (var t in startupAssembly.GetTypes()) { /* surfaces TypeLoadException early */ }

Try / catch

try { operations.GetContextTypes(); }
catch (OperationException ex) when (ex.Message.Contains("attempting to find 'DbContext' types"))
{ /* ex.InnerException names the failing type/assembly; restore/rename to fix */ }

Prevention

When it happens

Trigger: FindContextTypes walks assembly types and one of them throws during reflection (TypeLoadException, missing referenced assembly, static initializer fault). Common when the startup assembly references an assembly not present at design time.

Common situations: A plugin assembly referenced by the startup project is missing, an assembly has a static constructor that performs work that fails under the tool host, or version skew between assemblies.

Related errors


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

Appendix: source

Thrown at src/EFCore.Design/Design/Internal/DbContextOperations.cs:693

                {
                    var context = contextPair.Key;
                    contexts[context] = CreateContextFromServiceProvider(appServices, context);
                }
            }
        }
        catch (Exception ex)
        {
            if (ex is OperationException)
            {
                throw;
            }

            if (ex is TargetInvocationException)
            {
                ex = ex.InnerException!;
            }

            throw new OperationException(DesignStrings.CannotFindDbContextTypes(ex.Message), ex);
        }

        return contexts!;
    }

    private static Func<DbContext> CreateContextFromServiceProvider(IServiceProvider appServices, Type contextType)
    {
        var factoryInterface = typeof(IDbContextFactory<>).MakeGenericType(contextType);
        var factoryService = appServices.GetService(factoryInterface);
        return factoryService == null
            ? () => (DbContext)ActivatorUtilities.GetServiceOrCreateInstance(appServices, contextType)
            : () => (DbContext)factoryInterface
                .GetMethod(nameof(IDbContextFactory<DbContext>.CreateDbContext))
                !.Invoke(factoryService, null)!;
    }

    private Func<DbContext>? FindContextFactory(Type contextType)
    {

View on GitHub (pinned to 3a2006ef56)