{"record":{"id":"85cc32ff8915bd89","repo":"dotnet/efcore","slug":"more-than-one-dbcontext-was-found-specify-which-o","errorCode":null,"errorMessage":"More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.","messagePattern":"More than one DbContext was found\\. Specify which one to use\\. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":736,"sourceCode":"    private DbContext CreateContextFromFactory(Type factory, Type contextType)\n    {\n        _reporter.WriteVerbose(DesignStrings.UsingDbContextFactory(factory.ShortDisplayName()));\n\n        return (DbContext)typeof(IDesignTimeDbContextFactory<>).MakeGenericType(contextType)\n            .GetMethod(nameof(IDesignTimeDbContextFactory<DbContext>.CreateDbContext), [typeof(string[])])!\n            .Invoke(Activator.CreateInstance(factory), [_args])!;\n    }\n\n    private KeyValuePair<Type, Func<DbContext>> FindContextType(string? name)\n    {\n        var types = FindContextTypes(name);\n        return !string.IsNullOrEmpty(name)\n            ? types.First()\n            : types.Count switch\n            {\n                0 => throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name)),\n                1 => types.First(),\n                _ => throw new OperationException(DesignStrings.MultipleContexts)\n            };\n    }\n\n    private Dictionary<Type, Func<DbContext>?> FilterTypes(\n        Dictionary<Type, Func<DbContext>?> types,\n        string name,\n        bool throwOnEmpty)\n    {\n        var candidates = FilterTypes(types, name, StringComparison.OrdinalIgnoreCase);\n        if (candidates.Count == 0)\n        {\n            return throwOnEmpty\n                ? throw new OperationException(DesignStrings.NoContextWithName(name))\n                : candidates;\n        }\n\n        if (candidates.Count == 1)\n        {","sourceCodeStart":718,"sourceCodeEnd":754,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L718-L754","documentation":"Thrown by DbContextOperations.FindContextType when no name was specified and FindContextTypes returned more than one context (types.Count > 1). Without an explicit --context, the tool cannot choose between multiple DbContexts, so it requires the user to disambiguate.","triggerScenarios":"Running an EF tool command without --context in a project/assembly that exposes two or more DbContext types (via factories, attributes, type scan, or service provider). The tool refuses to guess.","commonSituations":"A solution with multiple bounded-context DbContexts (e.g., IdentityDbContext + AppDbContext). A project that references a library bringing its own context. Test/integration contexts that are also discoverable. Forgetting to pass --context after splitting a monolithic context.","solutions":["Specify the context: '--context AppDbContext' (PowerShell: '-Context AppDbContext').","If only one context is intended at design time, remove or hide the others from discovery (e.g., make them internal, move to a separate project, or gate with [DbContext] attribute on the desired one).","Use the fully-qualified type name if the short name is ambiguous across namespaces."],"exampleFix":"# before - multiple contexts, none specified\ndotnet ef migrations add Init   # throws MultipleContexts\n\n# after - specify which context\ndotnet ef migrations add Init --context AppDbContext","handlingStrategy":"validation","validationCode":"var types = ops.GetContextTypes().ToList();\nif (types.Count > 1)\n    throw new InvalidOperationException($\"Multiple contexts found ({string.Join(\", \", types.Select(t => t.Name))}); specify --context.\");","typeGuard":null,"tryCatchPattern":"try { var ctx = ops.CreateContext(null); }\ncatch (OperationException ex) when (ex.Message.Contains(\"More than one DbContext was found\"))\n{\n    // prompt the user for a context name, then call CreateContext(name)\n}","preventionTips":["Always pass --context when a project has more than one DbContext.","Use fully-qualified type names when short names clash across namespaces.","Reduce the number of discoverable contexts if only one is intended at design time."],"tags":["design","ef-tools","dbcontext","discovery","ambiguous"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}