{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L102-L138","documentation":"Thrown by DbContextOperations.DropDatabase when contextType is the wildcard '*' and no DbContext instances were found in the assembly (anyContext stayed false). The wildcard form is meant to drop the database for every context found, so finding zero is a usage error rather than a silent no-op.","triggerScenarios":"Running 'dotnet ef database drop --context *' (or the equivalent) when the startup/project assembly contains no discoverable DbContext types (none via factories, attributes, type scan, or service provider).","commonSituations":"Running the EF tools against the wrong project (a class library with no context, or the startup assembly is misconfigured). The DbContext lives in a referenced assembly that is not set as the startup assembly. The context is abstract or generic and is filtered out during scanning. Forgetting to add the [DbContext] attribute or IDesignTimeDbContextFactory when contexts are not directly constructible.","solutions":["Run the command from the project that contains (or references and registers) your DbContext, and set --startup-project correctly.","Add an IDesignTimeDbContextFactory<TContext> or [assembly: DbContext(typeof(TContext))] so the tool can discover the context.","Specify an explicit context name instead of the wildcard: '--context MyContext'.","Ensure the context class is non-abstract and non-generic."],"exampleFix":"// before - tool finds no contexts in the assembly\ndotnet ef database drop --context *\n\n// after - target the right startup project and name the context\ndotnet ef database drop --context MyContext --startup-project ./src/MyApp\n// or add a design-time factory in the startup assembly\npublic class MyContextFactory : IDesignTimeDbContextFactory<MyContext> { ... }","handlingStrategy":"validation","validationCode":"// Before wildcard drop, check that contexts are discoverable.\nvar ops = new DbContextOperations(reporter, assembly, startupAssembly, project, projectDir, ns, lang, nullable, args);\nvar types = ops.GetContextTypes().ToList();\nif (types.Count == 0)\n    throw new InvalidOperationException(\"No DbContext found; set --startup-project or add a design-time factory.\");","typeGuard":null,"tryCatchPattern":"try { ops.DropDatabase(\"*\", null); }\ncatch (OperationException ex) when (ex.Message.Contains(\"No DbContext was found\"))\n{\n    // fix discovery (startup-project / factory / attribute), then retry\n}","preventionTips":["Confirm the startup project contains or registers the DbContext.","Add IDesignTimeDbContextFactory<TContext> or [assembly: DbContext(typeof(TContext))].","Prefer an explicit --context name over the wildcard."],"tags":["design","ef-tools","dbcontext","discovery"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}