{"record":{"id":"8345e28a087f454d","repo":"microsoft/aspire","slug":"cannot-register-migrations-without-a-context-type-when-they-8345e2","errorCode":null,"errorMessage":"Cannot register migrations without a context type when they have already been registered for specific DbContext types on resource '{builder.Resource.Name}'.","messagePattern":"Cannot register migrations without a context type when they have already been registered for specific DbContext types on resource '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.EntityFrameworkCore/EFResourceBuilderExtensions.cs","lineNumber":175,"sourceCode":"                throw new InvalidOperationException(\n                    $\"The DbContext type '{GetShortTypeName(dbContextTypeName)}' has already been registered for EF migrations on resource '{builder.Resource.Name}'.\");\n            }\n\n            if (existingMigrationResources.Any(r => r.DbContextTypeName == null))\n            {\n                throw new InvalidOperationException(\n                    $\"Cannot register a specific DbContext type for migrations when they have already been registered without a context type on resource '{builder.Resource.Name}'.\");\n            }\n        }\n        else if (existingMigrationResources.Count != 0)\n        {\n            if (existingMigrationResources.Any(r => r.DbContextTypeName == null))\n            {\n                throw new InvalidOperationException(\n                     $\"Cannot register migrations without a context type when they have already been registered without a context type on resource '{builder.Resource.Name}'.\");\n            }\n            \n            throw new InvalidOperationException(\n                $\"Cannot register migrations without a context type when they have already been registered for specific DbContext types on resource '{builder.Resource.Name}'.\");\n        }\n\n        var migrationResource = new EFMigrationResource(name, builder.Resource, dbContextTypeName)\n        {\n            ConfigureToolResource = configureToolResource\n        };\n\n        var innerBuilder = builder.ApplicationBuilder\n            .AddResource(migrationResource)\n            .WithParentRelationship(builder)\n            .WithInitialState(new CustomResourceSnapshot\n            {\n                ResourceType = \"EFMigration\",\n                Properties = [],\n                State = new ResourceStateSnapshot(KnownResourceStates.NotStarted, KnownResourceStateStyles.Info)\n            })\n            .WithIconName(\"Database\")","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.EntityFrameworkCore/EFResourceBuilderExtensions.cs#L157-L193","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar pg = builder.AddPostgres(\"pg\").AddDatabase(\"db\");\npg.AddEFMigrations<MyDbContext>(\"m1\");\npg.AddEFMigrations(\"m2\"); // throws\n// after\npg.AddEFMigrations<MyDbContext>(\"m1\");\npg.AddEFMigrations<MyOtherDbContext>(\"m2\");","handlingStrategy":"validation","validationCode":"if (hasContextlessMigrations != wantsContextlessMigrations)\n    throw new InvalidOperationException(\"Use either typed or context-less AddEFMigrations per resource, not both.\");","typeGuard":null,"tryCatchPattern":"try { builder.AddEFMigrations(\"migrations\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot register migrations\")) { /* resolve duplicate registration */ }","preventionTips":["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"],"tags":["ef-core","migrations","aspire-hosting","invalid-state"],"backgroundTag":"conflicting-config-options","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}