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

  1. Point --startup-project at the assembly that registers/contains your DbContext.
  2. Add an IDesignTimeDbContextFactory<TContext> or [assembly: DbContext(typeof(TContext))].
  3. Specify the context explicitly: 'dotnet ef dbcontext optimize --context MyContext'.
  4. 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

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


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