{"record":{"id":"5931cc6da344ab79","repo":"microsoft/aspire","slug":"a-dbcontextoptions-typeof-tcontext-name-was-not-found-please","errorCode":null,"errorMessage":"A DbContextOptions<{typeof(TContext).Name}> was not found. Please ensure 'ServerVersion' was configured.","messagePattern":"A DbContextOptions<(.+?)> was not found\\. Please ensure 'ServerVersion' was configured\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.Pomelo.EntityFrameworkCore.MySql/AspireEFMySqlExtensions.cs","lineNumber":186,"sourceCode":"        void ConfigureRetry()\n        {\n#pragma warning disable EF1001 // Internal EF Core API usage.\n            if (!settings.DisableRetry || settings.CommandTimeout.HasValue)\n            {\n                builder.CheckDbContextRegistered<TContext>();\n\n#if NET9_0_OR_GREATER\n                builder.Services.ConfigureDbContext<TContext>(ConfigureRetryAndTimeout);\n#else\n                builder.PatchServiceDescriptor<TContext>(ConfigureRetryAndTimeout);\n#endif\n\n                void ConfigureRetryAndTimeout(DbContextOptionsBuilder optionsBuilder)\n                {\n                    if (optionsBuilder.Options.FindExtension<MySqlOptionsExtension>() is not MySqlOptionsExtension extension ||\n                        extension.ServerVersion is not ServerVersion serverVersion)\n                    {\n                        throw new InvalidOperationException($\"A DbContextOptions<{typeof(TContext).Name}> was not found. Please ensure 'ServerVersion' was configured.\");\n                    }\n\n                    optionsBuilder.UseMySql(serverVersion, options =>\n                    {\n                        var extension = optionsBuilder.Options.FindExtension<MySqlOptionsExtension>();\n\n                        if (!settings.DisableRetry)\n                        {\n                            var executionStrategy = extension?.ExecutionStrategyFactory?.Invoke(new ExecutionStrategyDependencies(null!, optionsBuilder.Options, null!));\n\n                            if (executionStrategy != null)\n                            {\n                                if (executionStrategy is MySqlRetryingExecutionStrategy)\n                                {\n                                    // Keep custom Retry strategy.\n                                    // Any sub-class of MySqlRetryingExecutionStrategy is a valid retry strategy\n                                    // which shouldn't be replaced even with DisableRetry == false\n                                }","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.Pomelo.EntityFrameworkCore.MySql/AspireEFMySqlExtensions.cs#L168-L204","documentation":"EnrichMySqlDbContext requires a MySqlOptionsExtension with a ServerVersion on the DbContextOptions so it can call UseMySql to configure retry/timeout. If the extension or ServerVersion is missing, it throws because Pomelo's provider cannot be (re)configured without a server version.","triggerScenarios":"Calling EnrichMySqlDbContext<TContext> where the context's options were not built with UseMySql including a server version — no MySqlOptionsExtension or extension.ServerVersion is null.","commonSituations":"DbContext registered with plain AddDbContext and options added outside UseMySql; server version supplied only later at runtime; using a different MySQL provider's extension; forgetting UseMySql in OnConfiguring before enrichment.","solutions":["Ensure UseMySql(connectionString, ServerVersion) is called when configuring the DbContext options, e.g. in AddDbContext or via the enrichment configure callback","Pass an explicit ServerVersion (e.g. ServerVersion.AutoDetect(connectionString) or new MySqlServerVersion(new Version(8, 0, 36)))","Verify the DbContext uses the Pomelo provider's options extension, not another provider's"],"exampleFix":"// before\nservices.AddDbContext<MyDbContext>(o => o.UseMySql(connectionString)); // no server version\nbuilder.EnrichMySqlDbContext<MyDbContext>();\n// after\nservices.AddDbContext<MyDbContext>(o =>\n    o.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));\nbuilder.EnrichMySqlDbContext<MyDbContext>();","handlingStrategy":"validation","validationCode":"var extension = dbContextOptions.Extensions.OfType<MySqlOptionsExtension>().FirstOrDefault();\nif (extension?.ServerVersion is null)\n    throw new InvalidOperationException(\"Call UseMySql(connectionString, serverVersion) before EnrichMySqlDbContext.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    builder.EnrichMySqlDbContext<MyDbContext>();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ServerVersion\"))\n{\n    logger.LogError(ex, \"DbContext options lack a MySQL server version.\");\n    throw;\n}","preventionTips":["Always call UseMySql with a ServerVersion (AutoDetect or explicit MySqlServerVersion)","Ensure the Pomelo provider extension is present, not another provider's","Configure the server version in one shared place so enrichment sees it"],"tags":["mysql","ef-core","server-version","pomelo","configuration"],"backgroundTag":"missing-required-config-field","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"}