Get started with the MySQL EF Core integration
MySQL is an open-source relational database management system (RDBMS) that uses SQL to manage and manipulate data. It’s widely used across environments ranging from small projects to large-scale enterprise systems, and is a popular choice for microservice data stores in cloud-native applications. The Aspire MySQL Pomelo Entity Framework Core (EF Core) integration lets you model a MySQL server and its databases as first-class resources in your AppHost, then connect to them from C# apps using EF Core.
Why use MySQL EF Core with Aspire
Section titled “Why use MySQL EF Core with Aspire”Adding MySQL through Aspire — rather than wiring up containers and connection strings by hand — gives you:
- Zero-config local development. Aspire runs MySQL from the
docker.io/library/mysqlcontainer image with credentials generated automatically for you. - Consistent connection info. Once you reference the database from a consuming app, Aspire injects connection properties as environment variables in a predictable format.
- Built-in health checks. The hosting integration registers a health check so the dashboard and your orchestrator can tell when the server is ready.
- Dashboard observability. The database resource appears in the Aspire dashboard with logs, status, and telemetry alongside your other services.
- First-class C# EF Core client integration. C# apps use the
Aspire.Pomelo.EntityFrameworkCore.MySqlpackage forDbContextregistration, health checks, and OpenTelemetry — all wired from the same resource name. - An upgrade path to managed Azure. The same AppHost model can be extended to managed MySQL offerings when you’re ready to deploy.
How the pieces fit together
Section titled “How the pieces fit together”The MySQL EF Core integration has two sides: a hosting integration that you use in your AppHost to model the database resource, and a C# client integration for consuming apps that register an EF Core DbContext.
architecture-beta group apphost(server)[AppHost] group consumer(server)[Consuming app] service hosting(server)[Hosting integration] in apphost service mysql(database)[MySQL server] in apphost service db(database)[mysqldb] in apphost service client(iconoir:server-connection)[EF Core client] in consumer service app(server)[App] in consumer hosting:R --> L:mysql mysql:R --> L:db db:R --> L:client client:R --> L:app
The hosting integration lives in your AppHost project and models the MySQL server and databases as resources. The client integration lives in each consuming C# app and uses EF Core to talk to the database.
Getting there is a two-step process: model the MySQL resources in your AppHost, then connect from each app that needs it.
-
Model MySQL in your AppHost
Section titled “Model MySQL in your AppHost”Add the MySQL hosting integration to your AppHost, then declare a MySQL server, one or more databases, and reference them from the apps that need to talk to the database. The MySQL Hosting integration article walks through every capability — adding databases, phpMyAdmin, data volumes, init scripts, and more.
Set up MySQL in the AppHost
-
Connect from your C# app
Section titled “Connect from your C# app”When you reference a MySQL database from a consuming app, Aspire injects its connection information. See Connect to MySQL with EF Core for the connection properties reference, EF Core
DbContextregistration, configuration options, health checks, and telemetry.Connect to MySQL with EF Core