dotnet/efcore · error · OperationException
No type deriving from DbContext was found. Add [assembly: Db
Error message
No type deriving from DbContext was found. Add [assembly: DbContext(typeof(*))] attribute for every context type used in this project.
What it means
Thrown by DbContextOperations.Optimize when contextTypeName is the wildcard '*' (optimizeAllInAssembly) and no context was actually optimized (contextOptimized stayed false). The optimize-all path expects at least one DbContext to exist in the assembly; finding none is treated as a configuration error.
Source
Thrown at src/EFCore.Design/Design/Internal/DbContextOperations.cs:201
outputDir,
modelNamespace,
suffix,
scaffoldModel,
precompileQueries,
context,
optimizeAllInAssembly,
nativeAot,
generatedFiles,
generatedFileNames);
contextOptimized = true;
}
}
if (optimizeAllInAssembly)
{
if (!contextOptimized)
{
throw new OperationException(DesignStrings.NoContextsToOptimize);
}
if (generatedFiles.Count == 0)
{
_reporter.WriteWarning(DesignStrings.OptimizeNoFilesGenerated);
}
}
return generatedFiles;
}
private void Optimize(
string? outputDir,
string? modelNamespace,
string? suffix,
bool scaffoldModel,
bool precompileQueries,
DbContext context,View on GitHub (pinned to dbf9771522)
Solutions
- Point --startup-project at the assembly that registers/contains your DbContext.
- Add an IDesignTimeDbContextFactory<TContext> or [assembly: DbContext(typeof(TContext))].
- Specify the context explicitly: 'dotnet ef dbcontext optimize --context MyContext'.
- Confirm the DbContext class is concrete and closed (non-abstract, non-generic).
Example fix
// before dotnet ef dbcontext optimize --context * // no contexts found // after dotnet ef dbcontext optimize --context MyContext --startup-project ./src/MyApp
Defensive patterns
Strategy: validation
Validate before calling
var types = ops.GetContextTypes().ToList();
if (types.Count == 0)
throw new InvalidOperationException("No DbContext to optimize; check --startup-project and discovery.");
ops.Optimize(outputDir, modelNamespace, "*", suffix, scaffoldModel, precompileQueries, nativeAot); Try / catch
try { ops.Optimize(outputDir, modelNamespace, "*", suffix, scaffoldModel, precompileQueries, nativeAot); }
catch (OperationException ex) when (ex.Message.Contains("No type deriving from DbContext"))
{
// fix discovery then retry
} Prevention
- Ensure a discoverable, concrete DbContext exists in the target assembly.
- Use --startup-project pointing at the application project.
- Specify an explicit context instead of the wildcard where possible.
When it happens
Trigger: Running 'dotnet ef dbcontext optimize --context *' (or 'optimize' with no specific context) when the assembly exposes no discoverable DbContext types through factories, attributes, type scanning, or the service provider.
Common situations: Running the optimize command in a project that has no DbContext yet. Misconfigured --startup-project pointing at a non-application assembly. Contexts that are abstract/generic and excluded from discovery. New codebase where the context has not been added.
Related errors
- No DbContext was found in assembly '{assembly}'. Ensure that
- The exception '{rootException}' was thrown while attempting
- 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/a082458ba3af83ec.
Report an issue: GitHub.