dotnet/efcore · error · OperationException

More than one DbContext named '{name}' was found. Specify wh

Error message

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

What it means

Multiple DbContext types match the given name case-insensitively, but none match case-sensitively. EF first does an OrdinalIgnoreCase pass; if that yields >1 it re-filters with Ordinal to disambiguate, and this error means the case-sensitive pass removed all of them. The fix is always to supply the exact casing of the intended type.

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

Solutions

  1. Run 'dotnet ef dbcontext list' and pass the name with the exact casing shown.
  2. If the duplicate is stale, rebuild/clean so the old type is no longer loaded.
  3. Use the fully-qualified name (namespace + type) if casing alone is ambiguous.

Example fix

// before
dotnet ef migrations add Init --context appdbcontext
// after
dotnet ef migrations add Init --context AppDbContext
Defensive patterns

Strategy: validation

Validate before calling

var matches = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(t => typeof(DbContext).IsAssignableFrom(t) && !t.IsAbstract)
    .Where(t => string.Equals(t.Name, contextName, StringComparison.Ordinal))
    .ToList();
if (matches.Count == 0) throw new ArgumentException("No exact-case match; fix casing.");

Prevention

When it happens

Trigger: Two contexts named e.g. 'AppDbContext' and 'AppdbContext' (or similar casing variants) coexist, and you pass a name whose casing matches neither exactly. FilterTypes OrdinalIgnoreCase -> count>1, then FilterTypes Ordinal -> count==0 -> throw MultipleContextsWithName.

Common situations: Renamed a context and the old one is still in a referenced assembly; case-only rename during a refactor; multiple contexts from third-party libraries whose names differ only in casing.

Related errors


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