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
GetMigrations with contextType=='*' iterated CreateAllContexts and found zero DbContext types in the target assembly, so anyContext stayed false. EF cannot list migrations because no context exists to host them. The message reminds that abstract or generic contexts are excluded from discovery.
Source
Thrown at src/EFCore.Design/Design/Internal/MigrationsOperations.cs:171
bool noConnect)
{
if (contextType == "*")
{
var anyContext = false;
var contextsList = new List<MigrationInfo>();
foreach (var contextItem in _contextOperations.CreateAllContexts())
{
anyContext = true;
using (contextItem)
{
contextsList.AddRange(GetMigrationsContext(contextItem, connectionString, noConnect));
}
}
if (!anyContext)
{
throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name));
}
}
using var context = _contextOperations.CreateContext(contextType);
{
return GetMigrationsContext(context, connectionString, noConnect);
}
}
private IEnumerable<MigrationInfo> GetMigrationsContext(DbContext context, string? connectionString, bool noConnect)
{
if (connectionString is not null)
{
context.Database.SetConnectionString(connectionString);
}
var services = _servicesBuilder.Build(context);
EnsureServices(services);View on GitHub (pinned to dbf9771522)
Solutions
- Confirm a public, concrete, non-generic DbContext subclass exists in the scanned assembly.
- Pass --project <assembly-with-context> so EF scans the right binary.
- Rebuild the project so the freshly compiled context type is loaded.
- Drop the '*' and pass an explicit context name once you know one exists.
Example fix
// before dotnet ef migrations list --context * // from Web project, context in Data project // after dotnet ef migrations list --context * -p ../Data -s .
Defensive patterns
Strategy: validation
Validate before calling
var contexts = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => typeof(DbContext).IsAssignableFrom(t) && !t.IsAbstract && !t.IsGenericType)
.ToList();
if (contexts.Count == 0) throw new InvalidOperationException("No discoverable DbContext in scanned assembly."); Type guard
static bool HasConcreteContext(Assembly asm) => asm.GetTypes()
.Any(t => typeof(DbContext).IsAssignableFrom(t) && !t.IsAbstract && !t.IsGenericType); Prevention
- Point --project at the assembly that defines the context.
- Keep contexts concrete and closed (no open generics).
- Rebuild before running design-time commands.
When it happens
Trigger: 'dotnet ef migrations list --context *' (or GetMigrations with wildcard) and CreateAllContexts yields nothing. The same guard exists so a wildcard run fails fast instead of silently producing empty output.
Common situations: Wrong assembly scanned (no --project pointing at the assembly containing the context); all contexts are abstract or generic-open; context is in a referenced class library not in the entry project; project not rebuilt after adding the context.
Related errors
- No DbContext named '{name}' was found.
- More than one DbContext named '{name}' was found. Specify wh
- More than one DbContext named '{name}' was found. Specify wh
- The wildcard '*' can only be used with commands that run for
- Changes have been made to the model since the last migration
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/67972f1a4188dc6d.
Report an issue: GitHub.