dotnet/efcore · error · OperationException

More than one DbContext was found. Specify which one to…

Error message

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.

What it means

OperationException MultipleContexts: no --context name was given but FindContextTypes returned more than one concrete DbContext. The tools cannot pick one unambiguously, so they refuse.

Solutions

  1. Specify --context MyContext (PowerShell: -Context) to disambiguate.
  2. Move secondary contexts to a separate assembly if one is the 'primary'.
  3. Use [assembly: DbContext(typeof(PrimaryContext))] only where helpful; for selection the explicit --context is the fix.

Example fix

# before
> dotnet ef migrations add Init   # two contexts present

# after
> dotnet ef migrations add Init --context MyApp.Data.AppDbContext
Defensive patterns

Strategy: validation

Validate before calling

var types = operations.GetContextTypes().ToList();
if (string.IsNullOrEmpty(name) && types.Count > 1)
    throw new InvalidOperationException($"Multiple contexts ({types.Count}); pass --context <FullName>.");

Try / catch

try { operations.CreateContext(null); }
catch (OperationException ex) when (ex.Message.Contains("More than one DbContext"))
{ /* pass --context <name> */ }

Prevention

When it happens

Trigger: 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.

Common situations: A multi-tenant or modular app with AppDbContext + IdentityDbContext + AuditDbContext in one assembly; the command worked before a second context was added.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/85cc32ff8915bd89. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Design/Design/Internal/DbContextOperations.cs:736

    private DbContext CreateContextFromFactory(Type factory, Type contextType)
    {
        _reporter.WriteVerbose(DesignStrings.UsingDbContextFactory(factory.ShortDisplayName()));

        return (DbContext)typeof(IDesignTimeDbContextFactory<>).MakeGenericType(contextType)
            .GetMethod(nameof(IDesignTimeDbContextFactory<DbContext>.CreateDbContext), [typeof(string[])])!
            .Invoke(Activator.CreateInstance(factory), [_args])!;
    }

    private KeyValuePair<Type, Func<DbContext>> FindContextType(string? name)
    {
        var types = FindContextTypes(name);
        return !string.IsNullOrEmpty(name)
            ? types.First()
            : types.Count switch
            {
                0 => throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name)),
                1 => types.First(),
                _ => throw new OperationException(DesignStrings.MultipleContexts)
            };
    }

    private Dictionary<Type, Func<DbContext>?> FilterTypes(
        Dictionary<Type, Func<DbContext>?> types,
        string name,
        bool throwOnEmpty)
    {
        var candidates = FilterTypes(types, name, StringComparison.OrdinalIgnoreCase);
        if (candidates.Count == 0)
        {
            return throwOnEmpty
                ? throw new OperationException(DesignStrings.NoContextWithName(name))
                : candidates;
        }

        if (candidates.Count == 1)
        {

View on GitHub (pinned to 3a2006ef56)