{"record":{"id":"b477826455f406eb","repo":"dotnet/efcore","slug":"the-exception-rootexception-was-thrown-while-a","errorCode":null,"errorMessage":"The exception '{rootException}' was thrown while attempting to find 'DbContext' types. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728","messagePattern":"The exception '(.+?)' was thrown while attempting to find 'DbContext' types\\. For the different patterns supported at design time, see https://go\\.microsoft\\.com/fwlink/\\?linkid=851728","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":693,"sourceCode":"                {\n                    var context = contextPair.Key;\n                    contexts[context] = CreateContextFromServiceProvider(appServices, context);\n                }\n            }\n        }\n        catch (Exception ex)\n        {\n            if (ex is OperationException)\n            {\n                throw;\n            }\n\n            if (ex is TargetInvocationException)\n            {\n                ex = ex.InnerException!;\n            }\n\n            throw new OperationException(DesignStrings.CannotFindDbContextTypes(ex.Message), ex);\n        }\n\n        return contexts!;\n    }\n\n    private static Func<DbContext> CreateContextFromServiceProvider(IServiceProvider appServices, Type contextType)\n    {\n        var factoryInterface = typeof(IDbContextFactory<>).MakeGenericType(contextType);\n        var factoryService = appServices.GetService(factoryInterface);\n        return factoryService == null\n            ? () => (DbContext)ActivatorUtilities.GetServiceOrCreateInstance(appServices, contextType)\n            : () => (DbContext)factoryInterface\n                .GetMethod(nameof(IDbContextFactory<DbContext>.CreateDbContext))\n                !.Invoke(factoryService, null)!;\n    }\n\n    private Func<DbContext>? FindContextFactory(Type contextType)\n    {","sourceCodeStart":675,"sourceCodeEnd":711,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L675-L711","documentation":"Thrown as OperationException by DbContextOperations.FindContextTypes when an unexpected (non-OperationException, non-TargetInvocationException after unwrap) exception occurs while discovering DbContext types. The original exception is wrapped with its message. This covers failures in the discovery pipeline itself: scanning assemblies, reading attributes, building the service provider, or invoking design-time factories.","triggerScenarios":"Any EF tooling operation that triggers context discovery when the discovery process throws. Examples: the startup assembly cannot be loaded, GetConstructibleTypes fails, the AppServiceProviderFactory.Create throws while building the host, or an IDesignTimeDbContextFactory.CreateDbContext throws during scanning.","commonSituations":"The startup project throws during service-provider construction (Program.Main / ConfigureServices). An assembly load failure (missing dependency, version mismatch). A design-time factory or hosting setup that fails at design time. Type-scanning hits a ReflectionTypeLoadException from a referenced assembly that is not present.","solutions":["Read the wrapped exception message to identify the root failure during discovery and resolve it (missing assembly, bad config, etc.).","Ensure --startup-project builds and runs cleanly; many discovery failures stem from a broken host build.","Add or fix an IDesignTimeDbContextFactory<TContext> so discovery does not depend on full host construction.","Restore all packages and verify referenced assemblies are present (resolve ReflectionTypeLoadException)."],"exampleFix":"// before - host throws during discovery because of a missing config/dependency\ndotnet ef migrations list  // CannotFindDbContextTypes wrapping the host error\n\n// after - add a design-time factory that bypasses host construction\npublic class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>\n{\n    public AppDbContext CreateDbContext(string[] args)\n    {\n        var opts = new DbContextOptionsBuilder<AppDbContext>()\n            .UseSqlServer(\"Server=.;Database=App;Trusted_Connection=True\");\n        return new AppDbContext(opts.Options);\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var types = ops.GetContextTypes(); }\ncatch (OperationException ex) when (ex.Message.Contains(\"was thrown while attempting to find 'DbContext' types\"))\n{\n    // inspect ex.InnerException for the root failure during discovery and resolve it\n}","preventionTips":["Ensure the startup project builds and runs without throwing during host construction.","Add an IDesignTimeDbContextFactory to bypass full host build during discovery.","Restore all packages so referenced assemblies load cleanly.","Resolve ReflectionTypeLoadException by providing missing referenced assemblies."],"tags":["design","ef-tools","dbcontext","discovery","startup"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}