MySqlConnector.DependencyInjection 2.3.5
About
MySqlConnector.DependencyInjection helps set up MySqlConnector in applications that use dependency injection, most notably in ASP.NET.
It allows easy configuration of your MySQL connections and registers the appropriate services in your DI container.
It also configures logging by integrating MySqlConnector with the ILoggingFactory
registered with the service provider.
How to Use
For example, if using the ASP.NET minimal web API, use the following to register MySqlConnector:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMySqlDataSource(builder.Configuration.GetConnectionString("Default"));
This registers a transient MySqlConnection
which can get injected into your controllers:
app.MapGet("/", async (MySqlConnection connection) =>
{
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = "SELECT name FROM users LIMIT 1";
return "Hello World: " + await command.ExecuteScalarAsync();
});
You can use MySqlDataSource
directly if you need more than one connection:
app.MapGet("/", async (MySqlDataSource dataSource) =>
{
await using var connection1 = await dataSource.OpenConnectionAsync();
await using var connection2 = await dataSource.OpenConnectionAsync();
// use the two connections...
});
Advanced Usage
The AddMySqlDataSource
method also accepts a lambda parameter allowing you to configure aspects of MySqlConnector beyond the connection string.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMySqlDataSource("Server=server;User ID=test;Password=test;Database=test",
x => x.UseRemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) => { /* custom logic */ })
);
Keyed Services
Use the AddKeyedMySqlDataSource
method to register a MySqlDataSource
as a keyed service.
This is useful if you have multiple connection strings or need to connect to multiple databases.
If the service key is a string, it will automatically be used as the MySqlDataSource
name;
to customize this, call the AddKeyedMySqlDataSource(object?, string, Action<MySqlDataSourceBuilder>)
overload and call MySqlDataSourceBuilder.UseName
.
builder.Services.AddKeyedMySqlDataSource("users", builder.Configuration.GetConnectionString("Users"));
builder.Services.AddKeyedMySqlDataSource("products", builder.Configuration.GetConnectionString("Products"));
app.MapGet("/users/{userId}", async (int userId, [FromKeyedServices("users")] MySqlConnection connection) =>
{
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = "SELECT name FROM users WHERE user_id = @userId LIMIT 1";
command.Parameters.AddWithValue("@userId", userId);
return $"Hello, {await command.ExecuteScalarAsync()}";
});
app.MapGet("/products/{productId}", async (int productId, [FromKeyedServices("products")] MySqlConnection connection) =>
{
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = "SELECT name FROM products WHERE product_id = @productId LIMIT 1";
command.Parameters.AddWithValue("@productId", productId);
return await command.ExecuteScalarAsync();
});
Showing the top 20 packages that depend on MySqlConnector.DependencyInjection.
Packages | Downloads |
---|---|
Aspire.MySqlConnector
A MySQL client that integrates with Aspire, including health checks, metrics, logging, and telemetry.
|
3 |
Aspire.MySqlConnector
A MySQL client that integrates with Aspire, including health checks, metrics, logging, and telemetry.
|
2 |
Aspire.MySqlConnector
A MySQL client that integrates with Aspire, including health checks, metrics, logging, and telemetry.
|
1 |
.NET 7.0
- MySqlConnector (>= 2.3.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
.NET Standard 2.0
- MySqlConnector (>= 2.3.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
Version | Downloads | Last updated |
---|---|---|
2.4.0 | 1 | 6/16/2025 |
2.4.0-beta.2 | 0 | 10/20/2024 |
2.4.0-beta.1 | 1 | 6/17/2025 |
2.3.6 | 0 | 3/20/2024 |
2.3.5 | 1 | 6/17/2025 |
2.3.1 | 0 | 11/17/2023 |
2.3.0 | 1 | 6/16/2025 |