{"record":{"id":"cb48cc9d264cc28f","repo":"dotnet/efcore","slug":"no-dbcontext-was-found-in-assembly-assembly-e","errorCode":null,"errorMessage":"No DbContext was found in assembly '{assembly}'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.","messagePattern":"No DbContext was found in assembly '(.+?)'\\. Ensure that you're using the correct assembly and that the type is neither abstract nor generic\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":120,"sourceCode":"    /// </summary>\n    public virtual void DropDatabase(string? contextType, string? connectionString)\n    {\n        if (contextType == \"*\")\n        {\n            var anyContext = false;\n\n            foreach (var contextItem in CreateAllContexts())\n            {\n                anyContext = true;\n                using (contextItem)\n                {\n                    DropDatabaseContext(contextItem, connectionString);\n                }\n            }\n\n            if (!anyContext)\n            {\n                throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name));\n            }\n\n            return;\n        }\n\n        using var context = CreateContext(contextType);\n        DropDatabaseContext(context, connectionString);\n    }\n\n    private void DropDatabaseContext(DbContext context, string? connectionString)\n    {\n        if (connectionString is not null)\n        {\n            context.Database.SetConnectionString(connectionString);\n        }\n\n        var connection = context.Database.GetDbConnection();\n        _reporter.WriteInformation(DesignStrings.DroppingDatabase(connection.Database, connection.DataSource));","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L102-L138","documentation":"OperationException thrown by DbContextOperations.DropDatabase when contextType == \"*\" (drop for every context in the assembly) but CreateAllContexts yielded no DbContext types. The wildcard loop ran zero times, so anyContext stays false.","triggerScenarios":"Running `dotnet ef database drop --context *` (or programmatic DropDatabase with contextType \"*\") in a startup/project assembly that contains no concrete, non-generic, non-abstract DbContext-derived types.","commonSituations":"Pointing the tools at the wrong assembly (a class library instead of the startup project), all contexts live behind a test/abstraction layer the discovery cannot instantiate, or DbContext types are abstract base classes only.","solutions":["Run the command from the project/assembly that actually contains the concrete DbContext type.","Specify the explicit context name instead of '*' (e.g. --context MyContext).","Add [assembly: DbContext(typeof(MyContext))] in the startup assembly so discovery finds it.","Ensure the context class is non-abstract and non-generic."],"exampleFix":"// before\n> dotnet ef database drop --context * --project src/MyApp.Domain\n\n// after\n> dotnet ef database drop --context MyApp.Data.AppDbContext --project src/MyApp.Api","handlingStrategy":"validation","validationCode":"var contexts = operations.GetContextTypes().ToList();\nif (contextType == \"*\" && contexts.Count == 0)\n{\n    throw new InvalidOperationException(\n        $\"No DbContext in assembly '{assemblyName}'; cannot drop for all contexts.\");\n}","typeGuard":null,"tryCatchPattern":"try { operations.DropDatabase(\"*\", null); }\ncatch (OperationException ex) when (ex.Message.Contains(\"No DbContext was found\"))\n{ /* point tooling at the correct assembly */ }","preventionTips":["Run EF tools from the startup project that owns the contexts.","Prefer naming --context explicitly over relying on '*'.","Add [assembly: DbContext(typeof(...))] to make discovery robust."],"tags":["design-time","ef-tools","context-discovery","drop-database"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}