dotnet/efcore · error · OperationException

No type deriving from DbContext was found. Add [assembly…

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

OperationException from DbContextOperations.Optimize when optimizeAllInAssembly is true (contextTypeName == "*") but no context was iterated, i.e. CreateAllContexts returned nothing. The command is meant to precompile/optimize every context but found none.

Solutions

  1. Run optimize from the assembly that contains the concrete DbContext type.
  2. Name the context explicitly instead of using '*'.
  3. Add [assembly: DbContext(typeof(MyContext))] so discovery succeeds.
  4. Make the target DbContext class a concrete (non-abstract, non-generic) class.

Example fix

// before
> dotnet ef dbcontext optimize --context *

// after
> dotnet ef dbcontext optimize --context MyApp.Data.AppDbContext
Defensive patterns

Strategy: validation

Validate before calling

var contexts = operations.GetContextTypes().ToList();
if (contextTypeName == "*" && contexts.Count == 0)
{
    throw new InvalidOperationException(
        "No contexts to optimize in this assembly; specify --context <name>.");
}

Try / catch

try { operations.Optimize(..., contextTypeName: "*", ...); }
catch (OperationException ex) when (ex.Message.Contains("No type deriving"))
{ /* target the assembly that holds the context */ }

Prevention

When it happens

Trigger: Running `dotnet ef optimize optimize --context *` (or dbcontext optimize with wildcard) against an assembly that has no discoverable concrete DbContext types.

Common situations: Wrong startup assembly, all contexts declared abstract, or running optimize before adding any DbContext to the project. Also after a refactor where contexts were moved to a referenced assembly not loaded by the tools.

Related errors


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

Appendix: 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 3a2006ef56)