microsoft/aspire · error · InvalidOperationException
Cannot register migrations without a context type when they…
Error message
Cannot register migrations without a context type when they have already been registered for specific DbContext types on resource '{builder.Resource.Name}'. What it means
Aspire lets you register EF Core migrations on a resource either globally (no DbContext type) or per specific DbContext types. This error is thrown by AddEFMigrationsCore when you try to add a context-type-less migration registration to a resource that already has migrations registered for specific DbContext types — the two registration styles are mutually exclusive.
Solutions
- Pick one registration style per resource: use typed AddEFMigrations<TContext>() for each context, or a single context-less AddEFMigrations()
- Remove the duplicate/ conflicting AddEFMigrations call so only typed or only context-less registrations remain
- Use a different target resource if migrations genuinely belong to a different project resource
Example fix
// before
var pg = builder.AddPostgres("pg").AddDatabase("db");
pg.AddEFMigrations<MyDbContext>("m1");
pg.AddEFMigrations("m2"); // throws
// after
pg.AddEFMigrations<MyDbContext>("m1");
pg.AddEFMigrations<MyOtherDbContext>("m2"); Defensive patterns
Strategy: validation
Validate before calling
if (hasContextlessMigrations != wantsContextlessMigrations)
throw new InvalidOperationException("Use either typed or context-less AddEFMigrations per resource, not both."); Try / catch
try { builder.AddEFMigrations("migrations"); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Cannot register migrations")) { /* resolve duplicate registration */ } Prevention
- Standardize on typed AddEFMigrations<TContext>() per context across the AppHost
- Centralize resource configuration in one extension method so migrations registration happens in exactly one place
- Search the AppHost for all AddEFMigrations calls on a resource before adding new ones
When it happens
Trigger: Calling builder.AddEFMigrations() (no DbContext type) on a resource that already has AddEFMigrations<TContext>(...) (typed) registrations, e.g. calling both AddEFMigrationsForPolyglot with typed contexts and then a context-less call on the same resource.
Common situations: Mixing the simple AddEFMigrations() convenience overload with per-context AddEFMigrations<TContext>() calls in the same AppHost; a team member adds a second migrations call without realizing one already exists elsewhere in the resource chain.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- Resource ' ' project metadata was replaced after project…
- A database name is required but was not provided. Specify…
- A DbContext could not be configured. Ensure valid…
- A DbContext could not be configured with this…
- A DbContextOptions< > was not found. Please ensure…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/8345e28a087f454d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.EntityFrameworkCore/EFResourceBuilderExtensions.cs:175
throw new InvalidOperationException(
$"The DbContext type '{GetShortTypeName(dbContextTypeName)}' has already been registered for EF migrations on resource '{builder.Resource.Name}'.");
}
if (existingMigrationResources.Any(r => r.DbContextTypeName == null))
{
throw new InvalidOperationException(
$"Cannot register a specific DbContext type for migrations when they have already been registered without a context type on resource '{builder.Resource.Name}'.");
}
}
else if (existingMigrationResources.Count != 0)
{
if (existingMigrationResources.Any(r => r.DbContextTypeName == null))
{
throw new InvalidOperationException(
$"Cannot register migrations without a context type when they have already been registered without a context type on resource '{builder.Resource.Name}'.");
}
throw new InvalidOperationException(
$"Cannot register migrations without a context type when they have already been registered for specific DbContext types on resource '{builder.Resource.Name}'.");
}
var migrationResource = new EFMigrationResource(name, builder.Resource, dbContextTypeName)
{
ConfigureToolResource = configureToolResource
};
var innerBuilder = builder.ApplicationBuilder
.AddResource(migrationResource)
.WithParentRelationship(builder)
.WithInitialState(new CustomResourceSnapshot
{
ResourceType = "EFMigration",
Properties = [],
State = new ResourceStateSnapshot(KnownResourceStates.NotStarted, KnownResourceStateStyles.Info)
})
.WithIconName("Database")View on GitHub (pinned to 25830f84bd)