dotnet/efcore · error · OperationException

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

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

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.

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 dbf9771522)

Solutions

  1. Specify the context: '--context AppDbContext' (PowerShell: '-Context AppDbContext').
  2. 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).
  3. Use the fully-qualified type name if the short name is ambiguous across namespaces.

Example fix

# before - multiple contexts, none specified
dotnet ef migrations add Init   # throws MultipleContexts

# after - specify which context
dotnet ef migrations add Init --context AppDbContext
Defensive patterns

Strategy: validation

Validate before calling

var types = ops.GetContextTypes().ToList();
if (types.Count > 1)
    throw new InvalidOperationException($"Multiple contexts found ({string.Join(", ", types.Select(t => t.Name))}); specify --context.");

Try / catch

try { var ctx = ops.CreateContext(null); }
catch (OperationException ex) when (ex.Message.Contains("More than one DbContext was found"))
{
    // prompt the user for a context name, then call CreateContext(name)
}

Prevention

When it happens

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

Common situations: 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.

Related errors


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