dotnet/efcore · error · OperationException

No DbContext named '{name}' was found.

Error message

No DbContext named '{name}' was found.

What it means

Thrown by FilterTypes when a context name was passed to a command (--context) but no DbContext type in the assembly matches that name, even case-insensitively. It comes from DbContextOperations.CreateContext, which is the single funnel used by every dotnet ef / PMC command that needs a DbContext. The error surfaces only when throwOnEmpty is true; otherwise the method returns an empty set and a later 'no context' error fires.

Source

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

            ? 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)
        {
            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;

View on GitHub (pinned to dbf9771522)

Solutions

  1. List contexts first: run 'dotnet ef dbcontext list' and copy the exact name shown.
  2. If none appear, fix the project being scanned -- pass --project <project-with-context> and --startup-project <startup-project>.
  3. Rebuild the startup project so the freshly compiled assembly containing the DbContext is loaded.
  4. If the context is generic or abstract, make it concrete or reference the concrete subclass.

Example fix

// before
dotnet ef migrations add Init --context AppDbcontext
// after (exact casing from 'dotnet ef dbcontext list')
dotnet ef migrations add Init --context AppDbContext
Defensive patterns

Strategy: validation

Validate before calling

// Before calling, enumerate context names and confirm the target exists.
var available = operations.CreateContextTypes().Select(t => t.Name).ToList();
if (!available.Contains(contextName, StringComparer.OrdinalIgnoreCase))
    throw new ArgumentException($"Unknown context '{contextName}'. Known: {string.Join(", ", available)}");

Prevention

When it happens

Trigger: Calling dotnet ef migrations add --context WrongName, or Database.GetMigrations/--context with a typo'd context name. CreateContext(name) -> FilterTypes(types, name, throwOnEmpty:true) finds zero candidates because none of t.Key.Name/FullName/AssemblyQualifiedName equal 'name' (OrdinalIgnoreCase) and throwOnEmpty is true.

Common situations: Typo in the context name; context lives in a different assembly than the one being scanned (wrong --startup-project/--project); context is nested/generic and its Name doesn't match what you typed; the assembly referenced is a stale build that predates the context being added.

Related errors


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