dotnet/efcore · error · OperationException

The exception '{rootException}' was thrown while attempting

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

Thrown as OperationException by DbContextOperations.FindContextTypes when an unexpected (non-OperationException, non-TargetInvocationException after unwrap) exception occurs while discovering DbContext types. The original exception is wrapped with its message. This covers failures in the discovery pipeline itself: scanning assemblies, reading attributes, building the service provider, or invoking design-time factories.

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 dbf9771522)

Solutions

  1. Read the wrapped exception message to identify the root failure during discovery and resolve it (missing assembly, bad config, etc.).
  2. Ensure --startup-project builds and runs cleanly; many discovery failures stem from a broken host build.
  3. Add or fix an IDesignTimeDbContextFactory<TContext> so discovery does not depend on full host construction.
  4. Restore all packages and verify referenced assemblies are present (resolve ReflectionTypeLoadException).

Example fix

// before - host throws during discovery because of a missing config/dependency
dotnet ef migrations list  // CannotFindDbContextTypes wrapping the host error

// after - add a design-time factory that bypasses host construction
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
    public AppDbContext CreateDbContext(string[] args)
    {
        var opts = new DbContextOptionsBuilder<AppDbContext>()
            .UseSqlServer("Server=.;Database=App;Trusted_Connection=True");
        return new AppDbContext(opts.Options);
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

try { var types = ops.GetContextTypes(); }
catch (OperationException ex) when (ex.Message.Contains("was thrown while attempting to find 'DbContext' types"))
{
    // inspect ex.InnerException for the root failure during discovery and resolve it
}

Prevention

When it happens

Trigger: Any EF tooling operation that triggers context discovery when the discovery process throws. Examples: the startup assembly cannot be loaded, GetConstructibleTypes fails, the AppServiceProviderFactory.Create throws while building the host, or an IDesignTimeDbContextFactory.CreateDbContext throws during scanning.

Common situations: The startup project throws during service-provider construction (Program.Main / ConfigureServices). An assembly load failure (missing dependency, version mismatch). A design-time factory or hosting setup that fails at design time. Type-scanning hits a ReflectionTypeLoadException from a referenced assembly that is not present.

Related errors


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