dotnet/efcore · error · OperationException

More than one DbContext named

Error message

More than one DbContext named '{name}' was found. Specify which one to use by providing its fully qualified name.

What it means

OperationException MultipleContextsWithName thrown when the name matched multiple contexts case-insensitively, then re-filtering with Ordinal (exact case) matched none. The user must supply a fully qualified name to disambiguate.

Solutions

  1. Pass the fully-qualified type name including its namespace: --context MyApp.Data.AppDbContext.
  2. Rename one of the colliding context types to remove the ambiguity.
  3. List contexts with `dotnet ef dbcontext list` to see the exact qualified names.

Example fix

# before - 'AppDbContext' exists in two namespaces, no exact-case match
> dotnet ef migrations add Init --context AppDbContext

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

Strategy: validation

Validate before calling

var ci = types.Where(t => t.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).ToList();
var cs = types.Where(t => t.Name.Equals(name, StringComparison.Ordinal)).ToList();
if (ci.Count > 1 && cs.Count == 0)
    throw new InvalidOperationException($"Name '{name}' matches multiple contexts only case-insensitively; use the fully-qualified name.");

Try / catch

try { operations.CreateContext(name); }
catch (OperationException ex) when (ex.Message.Contains("More than one DbContext named"))
{ /* supply the fully-qualified type name */ }

Prevention

When it happens

Trigger: Two contexts whose short/type names differ only in case across namespaces (e.g. Foo.Context vs Bar.context), or several contexts that share the same short name in different namespaces and the supplied name is the short one.

Common situations: Naming collisions after merging projects/modules, or accidental duplicate class names in separate namespaces.

Related errors


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

Appendix: source

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

    {
        var candidates = FilterTypes(types, name, StringComparison.OrdinalIgnoreCase);
        if (candidates.Count == 0)
        {
            return throwOnEmpty
                ? throw new OperationException(DesignStrings.NoContextWithName(name))
                : candidates;
        }

        if (candidates.Count == 1)
        {
            return candidates;
        }

        // Disambiguate using case
        candidates = FilterTypes(candidates, name, StringComparison.Ordinal);
        if (candidates.Count == 0)
        {
            throw new OperationException(DesignStrings.MultipleContextsWithName(name));
        }

        if (candidates.Count == 1)
        {
            return candidates;
        }

        // Allow selecting types in the default namespace
        candidates = candidates.Where(t => t.Key.Namespace == null).ToDictionary();
        if (candidates.Count == 0)
        {
            throw new OperationException(DesignStrings.MultipleContextsWithQualifiedName(name));
        }

        Check.DebugAssert(candidates.Count == 1, $"candidates.Count is {candidates.Count}");

        return candidates;
    }

View on GitHub (pinned to 3a2006ef56)