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
- Read the wrapped exception message to identify the root failure during discovery and resolve it (missing assembly, bad config, etc.).
- Ensure --startup-project builds and runs cleanly; many discovery failures stem from a broken host build.
- Add or fix an IDesignTimeDbContextFactory<TContext> so discovery does not depend on full host construction.
- 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
- Ensure the startup project builds and runs without throwing during host construction.
- Add an IDesignTimeDbContextFactory to bypass full host build during discovery.
- Restore all packages so referenced assemblies load cleanly.
- Resolve ReflectionTypeLoadException by providing missing referenced assemblies.
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
- No DbContext was found in assembly '{assembly}'. Ensure that
- No type deriving from DbContext was found. Add [assembly: Db
- More than one DbContext was found. Specify which one to use.
- The wildcard '*' can only be used with commands that run for
- Unable to create a 'DbContext' of type '{contextType}'. The
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/b477826455f406eb.
Report an issue: GitHub.