dotnet/efcore · error · OperationException

No DbContext was found in assembly

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

OperationException thrown by DbContextOperations.DropDatabase when contextType == "*" (drop for every context in the assembly) but CreateAllContexts yielded no DbContext types. The wildcard loop ran zero times, so anyContext stays false.

Solutions

  1. Run the command from the project/assembly that actually contains the concrete DbContext type.
  2. Specify the explicit context name instead of '*' (e.g. --context MyContext).
  3. Add [assembly: DbContext(typeof(MyContext))] in the startup assembly so discovery finds it.
  4. Ensure the context class is non-abstract and non-generic.

Example fix

// before
> dotnet ef database drop --context * --project src/MyApp.Domain

// after
> dotnet ef database drop --context MyApp.Data.AppDbContext --project src/MyApp.Api
Defensive patterns

Strategy: validation

Validate before calling

var contexts = operations.GetContextTypes().ToList();
if (contextType == "*" && contexts.Count == 0)
{
    throw new InvalidOperationException(
        $"No DbContext in assembly '{assemblyName}'; cannot drop for all contexts.");
}

Try / catch

try { operations.DropDatabase("*", null); }
catch (OperationException ex) when (ex.Message.Contains("No DbContext was found"))
{ /* point tooling at the correct assembly */ }

Prevention

When it happens

Trigger: Running `dotnet ef database drop --context *` (or programmatic DropDatabase with contextType "*") in a startup/project assembly that contains no concrete, non-generic, non-abstract DbContext-derived types.

Common situations: Pointing the tools at the wrong assembly (a class library instead of the startup project), all contexts live behind a test/abstraction layer the discovery cannot instantiate, or DbContext types are abstract base classes only.

Related errors


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

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