{"record":{"id":"67972f1a4188dc6d","repo":"dotnet/efcore","slug":"no-dbcontext-was-found-in-assembly-assembly-e-67972f","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/MigrationsOperations.cs","lineNumber":171,"sourceCode":"        bool noConnect)\n    {\n        if (contextType == \"*\")\n        {\n            var anyContext = false;\n            var contextsList = new List<MigrationInfo>();\n\n            foreach (var contextItem in _contextOperations.CreateAllContexts())\n            {\n                anyContext = true;\n                using (contextItem)\n                {\n                    contextsList.AddRange(GetMigrationsContext(contextItem, connectionString, noConnect));\n                }\n            }\n\n            if (!anyContext)\n            {\n                throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name));\n            }\n        }\n\n        using var context = _contextOperations.CreateContext(contextType);\n        {\n            return GetMigrationsContext(context, connectionString, noConnect);\n        }\n    }\n\n    private IEnumerable<MigrationInfo> GetMigrationsContext(DbContext context, string? connectionString, bool noConnect)\n    {\n        if (connectionString is not null)\n        {\n            context.Database.SetConnectionString(connectionString);\n        }\n\n        var services = _servicesBuilder.Build(context);\n        EnsureServices(services);","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/MigrationsOperations.cs#L153-L189","documentation":"GetMigrationsContext's caller iterated CreateAllContexts() (the wildcard path) and discovered zero instantiable DbContext types in the target assembly. EF treats 'no contexts at all' as a user-facing configuration error rather than silently producing empty output. The check uses anyContext so an assembly that contains contexts but none are usable does not trip this; it fires only when literally no context was enumerated.","triggerScenarios":"Calling the list/get-migrations path (e.g. 'dotnet ef migrations list --context *' or the PMC 'Get-Migration' equivalent) when the target assembly contains no concrete, non-generic DbContext subclasses. CreateAllContexts returns an empty enumerable, anyContext stays false, and the throw uses the assembly's simple name.","commonSituations":"Running EF tools against a class library or console project that references DbContext only transitively (contexts live in a different assembly); the DbContext class is abstract or open-generic (both are filtered out by context discovery); the wrong assembly was selected as the target project; the startup project builds but the assembly containing contexts hasn't been built.","solutions":["Verify the target/startup project is the one containing your concrete DbContext subclass (non-abstract, non-generic).","Build the project before running EF tooling so the assembly is present.","If contexts live in a separate assembly, set that assembly as the target via PMC 'Default project' dropdown or run dotnet ef from that project's directory.","Ensure the context class is not abstract or generic and inherits from DbContext."],"exampleFix":"// before: running from a project with no DbContext\ndotnet ef migrations list\n// after: point tooling at the project containing the context\ndotnet ef migrations list --project src/MyApp.Data --startup-project src/MyApp","handlingStrategy":"validation","validationCode":"// Confirm at least one usable context exists before the wildcard path\nvar contexts = dbContextAssembly.GetLoadableDefinedTypes()\n    .Where(t => typeof(DbContext).IsAssignableFrom(t) && !t.IsAbstract && !t.IsGenericType);\nif (!contexts.Any())\n{\n    throw new InvalidOperationException($\"No concrete DbContext in {dbContextAssembly.GetName().Name}.\");\n}","typeGuard":null,"tryCatchPattern":"try { ops.GetMigrations(...); }\ncatch (OperationException ex) when (ex.Message.StartsWith(\"No DbContext was found\"))\n{ /* point tooling at the assembly that defines the context */ }","preventionTips":["Run EF tooling from the project that defines the DbContext.","Keep contexts concrete and non-generic.","Build the target project before running EF tools."],"tags":["migrations","context-discovery","assembly","design-time"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}