dotnet/efcore · error · OperationException

No DbContext named ' ' was found.

Error message

No DbContext named '{name}' was found.

What it means

OperationException NoContextWithName thrown when a name was supplied but FilterTypes (case-insensitive) matched zero contexts. The user asked for a context that does not exist in the assembly.

Solutions

  1. List available contexts: `dotnet ef dbcontext list` and use the exact name shown.
  2. Run from the correct startup/project assembly.
  3. Use the fully-qualified type name to be unambiguous.
  4. Check for typos and case (case-insensitive but spelling must match).

Example fix

# before
> dotnet ef migrations add Init --context AppDbCtxt   # typo

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

Strategy: validation

Validate before calling

var matches = operations.GetContextTypes().Where(t => t.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).ToList();
if (matches.Count == 0)
    throw new InvalidOperationException($"No context named '{name}'; run 'dotnet ef dbcontext list'.");

Try / catch

try { operations.CreateContext(name); }
catch (OperationException ex) when (ex.Message.Contains("No DbContext named"))
{ /* list contexts and use the exact name */ }

Prevention

When it happens

Trigger: Passing --context with a typo, wrong case is fine (case-insensitive match attempted), but a name that matches no context type at all. Also when throwOnEmpty is true after filtering.

Common situations: Renamed context class, wrong namespace prefix used, copy-pasted command from another project, or pointing the tools at the wrong assembly.

Related errors


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

Appendix: 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 3a2006ef56)