| title | .NET Aspire Pomelo MySQL Entity Framework Core integration |
|---|---|
| description | Learn how to use the .NET Aspire MySQL Entity Framework integration, which includes both hosting and client integrations. |
| ms.date | 12/09/2024 |
[!INCLUDE includes-hosting-and-client]
MySQL is an open-source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL) to manage and manipulate data. It's employed in a many different environments, from small projects to large-scale enterprise systems and it's a popular choice to host data that underpins microservices in a cloud-native application. The .NET Aspire Pomelo MySQL Entity Framework Core integration enables you to connect to existing MySQL databases or create new instances from .NET with the mysql container image.
[!INCLUDE mysql-app-host]
To get started with the .NET Aspire Pomelo MySQL Entity Framework integration, install the 📦 Aspire.Pomelo.EntityFrameworkCore.MySql NuGet package in the client-consuming project, that is, the project for the application that uses the MySQL Entity Framework Core client.
dotnet add package Aspire.Pomelo.EntityFrameworkCore.MySql
<PackageReference Include="Aspire.Pomelo.EntityFrameworkCore.MySql"
Version="*" />For more information, see dotnet add package or Manage package dependencies in .NET applications.
In the :::no-loc text="Program.cs"::: file of your client-consuming project, call the xref:Microsoft.Extensions.Hosting.AspireEFMySqlExtensions.AddMySqlDbContext%2A extension method on any xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder to register a xref:Microsoft.EntityFrameworkCore.DbContext for use through the dependency injection container. The method takes a connection name parameter.
builder.AddMySqlDbContext<ExampleDbContext>(connectionName: "mysqldb");Tip
The connectionName parameter must match the name used when adding the SQL Server database resource in the app host project. In other words, when you call AddDatabase and provide a name of mysqldb that same name should be used when calling AddMySqlDbContext. For more information, see Add MySQL server resource and database resource.
To retrieve ExampleDbContext object from a service:
public class ExampleService(ExampleDbContext context)
{
// Use context...
}For more information on dependency injection, see .NET dependency injection.
To enrich the DbContext with additional services, such as automatic retries, health checks, logging and telemetry, call the xref:Microsoft.Extensions.Hosting.AspireEFMySqlExtensions.EnrichMySqlDbContext* method:
builder.EnrichMySqlDbContext<ExampleDbContext>(
connectionName: "mysqldb",
configureSettings: settings =>
{
settings.DisableRetry = false;
settings.CommandTimeout = 30 // seconds
});The settings parameter is an instance of the xref:Aspire.Pomelo.EntityFrameworkCore.MySql.PomeloEntityFrameworkCoreMySqlSettings class.
The .NET Aspire Pomelo MySQL Entity Framework Core integration provides multiple options to configure the database connection based on the requirements and conventions of your project.
When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddMySqlDatabaseDbContext<TContext>():
builder.AddMySqlDatabaseDbContext<MyDbContext>("mysql");And then the connection string will be retrieved from the ConnectionStrings configuration section:
{
"ConnectionStrings": {
"mysql": "Server=mysql;Database=mysqldb"
}
}The EnrichMySqlDbContext won't make use of the ConnectionStrings configuration section since it expects a DbContext to be registered at the point it's called.
For more information, see the MySqlConnector: ConnectionString documentation.
The .NET Aspire Pomelo MySQL Entity Framework Core integration supports xref:Microsoft.Extensions.Configuration?displayProperty=fullName. It loads the xref:Aspire.Pomelo.EntityFrameworkCore.MySql.PomeloEntityFrameworkCoreMySqlSettings from configuration files such as :::no-loc text="appsettings.json"::: by using the Aspire:Pomelo:EntityFrameworkCore:MySql key.
The following example shows an :::no-loc text="appsettings.json"::: that configures some of the available options:
{
"Aspire": {
"Pomelo": {
"EntityFrameworkCore": {
"MySql": {
"ConnectionString": "YOUR_CONNECTIONSTRING",
"DisableHealthChecks": true,
"DisableTracing": true
}
}
}
}
}For the complete MySQL integration JSON schema, see Aspire.Pomelo.EntityFrameworkCore.MySql/ConfigurationSchema.json.
You can also pass the Action<PomeloEntityFrameworkCoreMySqlSettings> delegate to set up some or all the options inline, for example to disable health checks from code:
builder.AddMySqlDbContext<MyDbContext>(
"mysqldb",
static settings => settings.DisableHealthChecks = true);or
builder.EnrichMySqlDbContext<MyDbContext>(
static settings => settings.DisableHealthChecks = true);[!INCLUDE client-integration-health-checks]
The .NET Aspire Pomelo MySQL Entity Framework Core integration:
- Adds the health check when xref:Aspire.Pomelo.EntityFrameworkCore.MySql.PomeloEntityFrameworkCoreMySqlSettings.DisableHealthChecks?displayProperty=nameWithType is
false, which calls EF Core's xref:Microsoft.EntityFrameworkCore.Storage.IDatabaseCreator.CanConnectAsync%2A method. - Integrates with the
/healthHTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.
[!INCLUDE integration-observability-and-telemetry]
The .NET Aspire Pomelo MySQL Entity Framework Core integration uses the following log categories:
Microsoft.EntityFrameworkCore.ChangeTrackingMicrosoft.EntityFrameworkCore.Database.CommandMicrosoft.EntityFrameworkCore.Database.ConnectionMicrosoft.EntityFrameworkCore.Database.TransactionMicrosoft.EntityFrameworkCore.InfrastructureMicrosoft.EntityFrameworkCore.MigrationsMicrosoft.EntityFrameworkCore.ModelMicrosoft.EntityFrameworkCore.Model.ValidationMicrosoft.EntityFrameworkCore.QueryMicrosoft.EntityFrameworkCore.Update
The .NET Aspire Pomelo MySQL Entity Framework Core integration will emit the following tracing activities using OpenTelemetry:
MySqlConnector
The .NET Aspire Pomelo MySQL Entity Framework Core integration currently supports the following metrics:
- MySqlConnector:
db.client.connections.create_timedb.client.connections.use_timedb.client.connections.wait_timedb.client.connections.idle.maxdb.client.connections.idle.mindb.client.connections.maxdb.client.connections.pending_requestsdb.client.connections.timeoutsdb.client.connections.usage