{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L718-L754","documentation":"OperationException MultipleContexts: no --context name was given but FindContextTypes returned more than one concrete DbContext. The tools cannot pick one unambiguously, so they refuse.","triggerScenarios":"An assembly contains two or more concrete DbContext-derived types and the user runs a single-context command without --context. The disambiguation name is null/empty so the switch hits the wildcard _ arm.","commonSituations":"A multi-tenant or modular app with AppDbContext + IdentityDbContext + AuditDbContext in one assembly; the command worked before a second context was added.","solutions":["Specify --context MyContext (PowerShell: -Context) to disambiguate.","Move secondary contexts to a separate assembly if one is the 'primary'.","Use [assembly: DbContext(typeof(PrimaryContext))] only where helpful; for selection the explicit --context is the fix."],"exampleFix":"# before\n> dotnet ef migrations add Init   # two contexts present\n\n# after\n> dotnet ef migrations add Init --context MyApp.Data.AppDbContext","handlingStrategy":"validation","validationCode":"var types = operations.GetContextTypes().ToList();\nif (string.IsNullOrEmpty(name) && types.Count > 1)\n    throw new InvalidOperationException($\"Multiple contexts ({types.Count}); pass --context <FullName>.\");","typeGuard":null,"tryCatchPattern":"try { operations.CreateContext(null); }\ncatch (OperationException ex) when (ex.Message.Contains(\"More than one DbContext\"))\n{ /* pass --context <name> */ }","preventionTips":["Always pass --context when an assembly may contain more than one DbContext.","Document the primary context in team scripts."],"tags":["design-time","ef-tools","context-discovery","ambiguous"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}