{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L183-L219","documentation":"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.","triggerScenarios":"Running `dotnet ef optimize optimize --context *` (or dbcontext optimize with wildcard) against an assembly that has no discoverable concrete DbContext types.","commonSituations":"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.","solutions":["Run optimize from the assembly that contains the concrete DbContext type.","Name the context explicitly instead of using '*'.","Add [assembly: DbContext(typeof(MyContext))] so discovery succeeds.","Make the target DbContext class a concrete (non-abstract, non-generic) class."],"exampleFix":"// before\n> dotnet ef dbcontext optimize --context *\n\n// after\n> dotnet ef dbcontext optimize --context MyApp.Data.AppDbContext","handlingStrategy":"validation","validationCode":"var contexts = operations.GetContextTypes().ToList();\nif (contextTypeName == \"*\" && contexts.Count == 0)\n{\n    throw new InvalidOperationException(\n        \"No contexts to optimize in this assembly; specify --context <name>.\");\n}","typeGuard":null,"tryCatchPattern":"try { operations.Optimize(..., contextTypeName: \"*\", ...); }\ncatch (OperationException ex) when (ex.Message.Contains(\"No type deriving\"))\n{ /* target the assembly that holds the context */ }","preventionTips":["Confirm at least one concrete DbContext exists before running optimize with '*'.","Keep the startup project the home of the context types."],"tags":["design-time","ef-tools","context-discovery","optimize","native-aot"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}