{"record":{"id":"a082458ba3af83ec","repo":"dotnet/efcore","slug":"no-type-deriving-from-dbcontext-was-found-add-as","errorCode":null,"errorMessage":"No type deriving from DbContext was found. Add [assembly: DbContext(typeof(*))] attribute for every context type used in this project.","messagePattern":"No type deriving from DbContext was found\\. Add \\[assembly: DbContext\\(typeof\\(\\*\\)\\)\\] attribute for every context type used in this project\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":201,"sourceCode":"                    outputDir,\n                    modelNamespace,\n                    suffix,\n                    scaffoldModel,\n                    precompileQueries,\n                    context,\n                    optimizeAllInAssembly,\n                    nativeAot,\n                    generatedFiles,\n                    generatedFileNames);\n                contextOptimized = true;\n            }\n        }\n\n        if (optimizeAllInAssembly)\n        {\n            if (!contextOptimized)\n            {\n                throw new OperationException(DesignStrings.NoContextsToOptimize);\n            }\n\n            if (generatedFiles.Count == 0)\n            {\n                _reporter.WriteWarning(DesignStrings.OptimizeNoFilesGenerated);\n            }\n        }\n\n        return generatedFiles;\n    }\n\n    private void Optimize(\n        string? outputDir,\n        string? modelNamespace,\n        string? suffix,\n        bool scaffoldModel,\n        bool precompileQueries,\n        DbContext context,","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L183-L219","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\ndotnet ef dbcontext optimize --context *   // no contexts found\n\n// after\ndotnet ef dbcontext optimize --context MyContext --startup-project ./src/MyApp","handlingStrategy":"validation","validationCode":"var types = ops.GetContextTypes().ToList();\nif (types.Count == 0)\n    throw new InvalidOperationException(\"No DbContext to optimize; check --startup-project and discovery.\");\nops.Optimize(outputDir, modelNamespace, \"*\", suffix, scaffoldModel, precompileQueries, nativeAot);","typeGuard":null,"tryCatchPattern":"try { ops.Optimize(outputDir, modelNamespace, \"*\", suffix, scaffoldModel, precompileQueries, nativeAot); }\ncatch (OperationException ex) when (ex.Message.Contains(\"No type deriving from DbContext\"))\n{\n    // fix discovery then retry\n}","preventionTips":["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."],"tags":["design","ef-tools","optimize","dbcontext","discovery"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}