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
GetMigrationsContext's caller iterated CreateAllContexts() (the wildcard path) and discovered zero instantiable DbContext types in the target assembly. EF treats 'no contexts at all' as a user-facing configuration error rather than silently producing empty output. The check uses anyContext so an assembly that contains contexts but none are usable does not trip this; it fires only when literally no context was enumerated.
Solutions
- Verify the target/startup project is the one containing your concrete DbContext subclass (non-abstract, non-generic).
- Build the project before running EF tooling so the assembly is present.
- If contexts live in a separate assembly, set that assembly as the target via PMC 'Default project' dropdown or run dotnet ef from that project's directory.
- Ensure the context class is not abstract or generic and inherits from DbContext.
Example fix
// before: running from a project with no DbContext dotnet ef migrations list // after: point tooling at the project containing the context dotnet ef migrations list --project src/MyApp.Data --startup-project src/MyApp
Defensive patterns
Strategy: validation
Validate before calling
// Confirm at least one usable context exists before the wildcard path
var contexts = dbContextAssembly.GetLoadableDefinedTypes()
.Where(t => typeof(DbContext).IsAssignableFrom(t) && !t.IsAbstract && !t.IsGenericType);
if (!contexts.Any())
{
throw new InvalidOperationException($"No concrete DbContext in {dbContextAssembly.GetName().Name}.");
} Try / catch
try { ops.GetMigrations(...); }
catch (OperationException ex) when (ex.Message.StartsWith("No DbContext was found"))
{ /* point tooling at the assembly that defines the context */ } Prevention
- Run EF tooling from the project that defines the DbContext.
- Keep contexts concrete and non-generic.
- Build the target project before running EF tools.
When it happens
Trigger: Calling the list/get-migrations path (e.g. 'dotnet ef migrations list --context *' or the PMC 'Get-Migration' equivalent) when the target assembly contains no concrete, non-generic DbContext subclasses. CreateAllContexts returns an empty enumerable, anyContext stays false, and the throw uses the assembly's simple name.
Common situations: Running EF tools against a class library or console project that references DbContext only transitively (contexts live in a different assembly); the DbContext class is abstract or open-generic (both are filtered out by context discovery); the wrong assembly was selected as the target project; the startup project builds but the assembly containing contexts hasn't been built.
Related errors
- Changes have been made to the model since the last…
- More than one DbContext named
- More than one DbContext named
- More than one DbContext was found. Specify which one to…
- No DbContext named ' ' was found.
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/67972f1a4188dc6d.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)