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 using its exact case.

What it means

Multiple DbContext types share the exact same Name (case-sensitive) and live in different namespaces, and none of them has a null namespace, so EF cannot pick one. Even the fully-qualified-name match was ambiguous. You must identify the context by its exact fully qualified (or assembly-qualified) name.

Source

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

        }

        // 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;
    }

    private static Dictionary<Type, Func<DbContext>?> FilterTypes(
        Dictionary<Type, Func<DbContext>?> types,
        string name,
        StringComparison comparisonType)
        => types
            .Where(t => string.Equals(t.Key.Name, name, comparisonType)
                || string.Equals(t.Key.FullName, name, comparisonType)
                || string.Equals(t.Key.AssemblyQualifiedName, name, comparisonType))
            .ToDictionary();
}

View on GitHub (pinned to dbf9771522)

Solutions

  1. Pass the fully qualified name: --context MyNamespace.AppDbContext.
  2. If still ambiguous, pass the AssemblyQualifiedName (use 'dotnet ef dbcontext list --json' to see full names).
  3. Rename one of the colliding types so they are unambiguous.
  4. Remove a stale project reference that brings in the duplicate.

Example fix

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

Strategy: validation

Validate before calling

var candidates = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(t => typeof(DbContext).IsAssignableFrom(t) && !t.IsAbstract && t.Name == contextName)
    .ToList();
if (candidates.Count > 1)
    contextName = candidates.Single(t => t.Namespace == wantedNamespace).FullName;

Prevention

When it happens

Trigger: Two types literally named 'AppDbContext' in namespaces A and B both pass the Ordinal filter (count>1), and the null-namespace tiebreaker at line 771 also yields >1 (or 0, triggering this throw). Reached only when short name + casing both match multiple types.

Common situations: Two projects in the same solution each define MyApp.AppDbContext; a duplicated context copied across folders; linking the same context under different namespaces via internals-visible-to tricks.

Related errors


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