dotnet/efcore · error · OperationException

No DbContext was found in assembly '{assembly}'. Ensure that

Error message

No DbContext was found in assembly '{assembly}'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

What it means

Thrown by DbContextOperations.DropDatabase when contextType is the wildcard '*' and no DbContext instances were found in the assembly (anyContext stayed false). The wildcard form is meant to drop the database for every context found, so finding zero is a usage error rather than a silent no-op.

Source

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

    /// </summary>
    public virtual void DropDatabase(string? contextType, string? connectionString)
    {
        if (contextType == "*")
        {
            var anyContext = false;

            foreach (var contextItem in CreateAllContexts())
            {
                anyContext = true;
                using (contextItem)
                {
                    DropDatabaseContext(contextItem, connectionString);
                }
            }

            if (!anyContext)
            {
                throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name));
            }

            return;
        }

        using var context = CreateContext(contextType);
        DropDatabaseContext(context, connectionString);
    }

    private void DropDatabaseContext(DbContext context, string? connectionString)
    {
        if (connectionString is not null)
        {
            context.Database.SetConnectionString(connectionString);
        }

        var connection = context.Database.GetDbConnection();
        _reporter.WriteInformation(DesignStrings.DroppingDatabase(connection.Database, connection.DataSource));

View on GitHub (pinned to dbf9771522)

Solutions

  1. Run the command from the project that contains (or references and registers) your DbContext, and set --startup-project correctly.
  2. Add an IDesignTimeDbContextFactory<TContext> or [assembly: DbContext(typeof(TContext))] so the tool can discover the context.
  3. Specify an explicit context name instead of the wildcard: '--context MyContext'.
  4. Ensure the context class is non-abstract and non-generic.

Example fix

// before - tool finds no contexts in the assembly
dotnet ef database drop --context *

// after - target the right startup project and name the context
dotnet ef database drop --context MyContext --startup-project ./src/MyApp
// or add a design-time factory in the startup assembly
public class MyContextFactory : IDesignTimeDbContextFactory<MyContext> { ... }
Defensive patterns

Strategy: validation

Validate before calling

// Before wildcard drop, check that contexts are discoverable.
var ops = new DbContextOperations(reporter, assembly, startupAssembly, project, projectDir, ns, lang, nullable, args);
var types = ops.GetContextTypes().ToList();
if (types.Count == 0)
    throw new InvalidOperationException("No DbContext found; set --startup-project or add a design-time factory.");

Try / catch

try { ops.DropDatabase("*", null); }
catch (OperationException ex) when (ex.Message.Contains("No DbContext was found"))
{
    // fix discovery (startup-project / factory / attribute), then retry
}

Prevention

When it happens

Trigger: Running 'dotnet ef database drop --context *' (or the equivalent) when the startup/project assembly contains no discoverable DbContext types (none via factories, attributes, type scan, or service provider).

Common situations: Running the EF tools against the wrong project (a class library with no context, or the startup assembly is misconfigured). The DbContext lives in a referenced assembly that is not set as the startup assembly. The context is abstract or generic and is filtered out during scanning. Forgetting to add the [DbContext] attribute or IDesignTimeDbContextFactory when contexts are not directly constructible.

Related errors


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