microsoft/aspire · error · InvalidOperationException

A DbContextOptions< > was not found. Please ensure…

Error message

A DbContextOptions<{typeof(TContext).Name}> was not found. Please ensure 'ServerVersion' was configured.

What it means

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.

Solutions

  1. Ensure UseMySql(connectionString, ServerVersion) is called when configuring the DbContext options, e.g. in AddDbContext or via the enrichment configure callback
  2. Pass an explicit ServerVersion (e.g. ServerVersion.AutoDetect(connectionString) or new MySqlServerVersion(new Version(8, 0, 36)))
  3. Verify the DbContext uses the Pomelo provider's options extension, not another provider's

Example fix

// before
services.AddDbContext<MyDbContext>(o => o.UseMySql(connectionString)); // no server version
builder.EnrichMySqlDbContext<MyDbContext>();
// after
services.AddDbContext<MyDbContext>(o =>
    o.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
builder.EnrichMySqlDbContext<MyDbContext>();
Defensive patterns

Strategy: validation

Validate before calling

var extension = dbContextOptions.Extensions.OfType<MySqlOptionsExtension>().FirstOrDefault();
if (extension?.ServerVersion is null)
    throw new InvalidOperationException("Call UseMySql(connectionString, serverVersion) before EnrichMySqlDbContext.");

Try / catch

try
{
    builder.EnrichMySqlDbContext<MyDbContext>();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("ServerVersion"))
{
    logger.LogError(ex, "DbContext options lack a MySQL server version.");
    throw;
}

Prevention

When it happens

Trigger: Calling EnrichMySqlDbContext<TContext> where the context's options were not built with UseMySql including a server version — no MySqlOptionsExtension or extension.ServerVersion is null.

Common situations: 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.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/5931cc6da344ab79. Report an issue: GitHub.

Appendix: source

Thrown at src/Components/Aspire.Pomelo.EntityFrameworkCore.MySql/AspireEFMySqlExtensions.cs:186

        void ConfigureRetry()
        {
#pragma warning disable EF1001 // Internal EF Core API usage.
            if (!settings.DisableRetry || settings.CommandTimeout.HasValue)
            {
                builder.CheckDbContextRegistered<TContext>();

#if NET9_0_OR_GREATER
                builder.Services.ConfigureDbContext<TContext>(ConfigureRetryAndTimeout);
#else
                builder.PatchServiceDescriptor<TContext>(ConfigureRetryAndTimeout);
#endif

                void ConfigureRetryAndTimeout(DbContextOptionsBuilder optionsBuilder)
                {
                    if (optionsBuilder.Options.FindExtension<MySqlOptionsExtension>() is not MySqlOptionsExtension extension ||
                        extension.ServerVersion is not ServerVersion serverVersion)
                    {
                        throw new InvalidOperationException($"A DbContextOptions<{typeof(TContext).Name}> was not found. Please ensure 'ServerVersion' was configured.");
                    }

                    optionsBuilder.UseMySql(serverVersion, options =>
                    {
                        var extension = optionsBuilder.Options.FindExtension<MySqlOptionsExtension>();

                        if (!settings.DisableRetry)
                        {
                            var executionStrategy = extension?.ExecutionStrategyFactory?.Invoke(new ExecutionStrategyDependencies(null!, optionsBuilder.Options, null!));

                            if (executionStrategy != null)
                            {
                                if (executionStrategy is MySqlRetryingExecutionStrategy)
                                {
                                    // Keep custom Retry strategy.
                                    // Any sub-class of MySqlRetryingExecutionStrategy is a valid retry strategy
                                    // which shouldn't be replaced even with DisableRetry == false
                                }

View on GitHub (pinned to 25830f84bd)