{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L675-L711","documentation":"OperationException when scanning the assembly for DbContext types itself threw an exception (other than an already-wrapped OperationException). The original exception message is wrapped; this indicates discovery logic, not context construction, failed.","triggerScenarios":"FindContextTypes walks assembly types and one of them throws during reflection (TypeLoadException, missing referenced assembly, static initializer fault). Common when the startup assembly references an assembly not present at design time.","commonSituations":"A plugin assembly referenced by the startup project is missing, an assembly has a static constructor that performs work that fails under the tool host, or version skew between assemblies.","solutions":["Read the wrapped inner exception to find the failing type/assembly.","Ensure all referenced assemblies are present (run `dotnet restore`/publish dependencies).","Reduce the startup assembly surface, or move contexts to a dedicated assembly so discovery walks fewer types.","Catch and guard problematic static initializers so they do not fault during reflection."],"exampleFix":"// before - static initializer throws during type scan\npublic class PluginModule { static PluginModule() { File.ReadAllText(\"missing\"); } }\n\n// after - lazy/guarded initialization\npublic class PluginModule { static PluginModule() { } public void Init() { ... } }","handlingStrategy":"try-catch","validationCode":"// sanity-scan the startup assembly for load issues before invoking the tools\nforeach (var t in startupAssembly.GetTypes()) { /* surfaces TypeLoadException early */ }","typeGuard":null,"tryCatchPattern":"try { operations.GetContextTypes(); }\ncatch (OperationException ex) when (ex.Message.Contains(\"attempting to find 'DbContext' types\"))\n{ /* ex.InnerException names the failing type/assembly; restore/rename to fix */ }","preventionTips":["Keep the startup assembly's dependency graph closed (all references resolvable).","Avoid heavy work in static constructors.","Run `dotnet restore` so all transitive assemblies load."],"tags":["design-time","ef-tools","context-discovery","reflection","assembly-load"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}