microsoft/aspire · error · InvalidOperationException
A database name is required but was not provided. Specify…
Error message
A database name is required but was not provided. Specify it via the '{nameof(databaseName)}' parameter, the '{nameof(MongoDBEntityFrameworkCoreSettings.DatabaseName)}' setting, or include it in the connection string. What it means
MongoDB requires a database name for UseMongoDB(connectionString, databaseName). The AddMongoDbContext ConfigureDbContext callback validates the connection string first, then throws if settings.DatabaseName is still null/empty — meaning the databaseName parameter, the MongoDBEntityFrameworkCoreSettings.DatabaseName setting, and the 'Database=' part of the connection string were all absent.
Solutions
- Use the AddMongoDbContext overload that takes a databaseName parameter.
- Or append the database to the connection string: mongodb+srv://.../mydb.
- Or set DatabaseName in the 'Aspire:MongoDB:EntityFrameworkCore' configuration section (or the :<TContext.Name> override).
- Ensure the EF design-time factory also supplies a database name when running 'dotnet ef' commands.
Example fix
// before
builder.AddMongoDbContext<CatalogContext>("mongo"); // connection string has no /db
// after
builder.AddMongoDbContext<CatalogContext>("mongo", "catalog");
// or: "ConnectionStrings": { "mongo": "mongodb+srv://user:pass@cluster/catalog" } Defensive patterns
Strategy: validation
Validate before calling
// before AddMongoDbContext without a databaseName parameter
var cs = builder.Configuration.GetConnectionString(connectionName) ?? "";
var dbNameFromCs = cs.Split('/').LastOrDefault(p => p.Contains('?') ? p.Split('?')[0].Length > 0 : p.Length > 0);
var settingDb = builder.Configuration["Aspire:MongoDB:EntityFrameworkCore:DatabaseName"];
if (string.IsNullOrEmpty(dbNameFromCs) && string.IsNullOrEmpty(settingDb))
{
throw new InvalidOperationException("MongoDB database name must come from the parameter, settings, or connection string.");
} Type guard
bool HasMongoDatabaseName(string? param, string? settingDb, string? cs) => !string.IsNullOrEmpty(param) || !string.IsNullOrEmpty(settingDb) || (cs?.Split('/').LastOrDefault()?.Split('?')[0].Length > 0); Try / catch
try { builder.AddMongoDbContext<CatalogContext>("mongo"); }
catch (InvalidOperationException ex) when (ex.Message.Contains("A database name is required")) { builder.AddMongoDbContext<CatalogContext>("mongo", "catalog"); } Prevention
- Pass the databaseName parameter explicitly instead of relying on connection-string inference.
- When copying Atlas connection strings, remember to append /databasename before any '?'.
- If you use EF design-time factories, give them a database name too or 'dotnet ef' will fail.
When it happens
Trigger: Calling AddMongoDbContext<TContext>(connectionName) without a databaseName argument where the connection string has no database part and the settings section lacks DatabaseName; validation happens lazily when the DbContext options are configured.
Common situations: Connection string copied from Atlas (e.g. mongodb+srv://user:pass@cluster/) without /dbname; refactor from the named overload to the inferred overload; DatabaseName key typo'd in the 'Aspire:MongoDB:EntityFrameworkCore' section; EF design-time factory generating options with a bare connection string.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- A DbContext could not be configured. Ensure valid…
- A DbContext could not be configured with this…
- A DbContextOptions< > was not found. Please ensure…
- Failed to configure MongoDB replica set resource
- . needs to be set when a custom Execution Strategy is…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/b417330ac07209c4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Aspire.MongoDB.EntityFrameworkCore/AspireMongoDBEntityFrameworkCoreExtensions.cs:86
if (!string.IsNullOrEmpty(mongoUrl.DatabaseName))
{
settings.DatabaseName = mongoUrl.DatabaseName;
}
}
configureSettings?.Invoke(settings);
builder.Services.AddDbContextPool<TContext>(ConfigureDbContext);
ConfigureInstrumentation<TContext>(builder, settings);
void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
{
ConnectionStringValidation.ValidateConnectionString(settings.ConnectionString, connectionName, DefaultConfigSectionName, $"{DefaultConfigSectionName}:{typeof(TContext).Name}", isEfDesignTime: EF.IsDesignTime);
if (string.IsNullOrEmpty(settings.DatabaseName))
{
throw new InvalidOperationException($"A database name is required but was not provided. Specify it via the '{nameof(databaseName)}' parameter, the '{nameof(MongoDBEntityFrameworkCoreSettings.DatabaseName)}' setting, or include it in the connection string.");
}
dbContextOptionsBuilder.UseMongoDB(settings.ConnectionString!, settings.DatabaseName);
configureDbContextOptions?.Invoke(dbContextOptionsBuilder);
}
}
/// <summary>
/// Configures logging and telemetry for the <see cref="DbContext" />.
/// </summary>
/// <typeparam name="TContext">The <see cref="DbContext" /> that needs to be configured.</typeparam>
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
/// <param name="configureSettings">An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.</param>
/// <remarks>Reads the configuration from "Aspire:MongoDB:EntityFrameworkCore:{typeof(TContext).Name}" config section, or "Aspire:MongoDB:EntityFrameworkCore" if former does not exist.</remarks>
/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="DbContext"/> is not registered in DI.</exception>
public static void EnrichMongoDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(View on GitHub (pinned to 25830f84bd)