MySQL
This connector simplifies accessing MySQL databases. It supports the following .NET drivers:
- MySqlConnector, which provides an ADO.NET
DbConnection
. - MySql.Data, which provides an ADO.NET
DbConnection
. - Pomelo.EntityFrameworkCore.MySql, which provides Entity Framework Core support.
- MySql.EntityFrameworkCore, which provides Entity Framework Core support.
The remainder of this page assumes you're familiar with the basic concepts of Steeltoe Connectors.
Usage
To use this connector:
- Create a MySQL server instance or use a docker container.
- Add NuGet references to your project.
- Configure your connection string in
appsettings.json
. - Initialize the Steeltoe Connector at startup.
- Use the driver-specific connection/client instance.
Add NuGet References
To use this connector, add a NuGet reference to Steeltoe.Connectors
. If you're using Entity Framework Core, add a
NuGet reference to Steeltoe.Connectors.EntityFrameworkCore
instead.
Also add a NuGet reference to one of the .NET drivers listed above, as you would if you were not using Steeltoe.
Configure connection string
The available connection string parameters for MySQL are documented here and here.
The following example appsettings.json
uses the docker container from above:
{
"Steeltoe": {
"Client": {
"MySql": {
"Default": {
"ConnectionString": "Server=localhost;Database=steeltoe;Uid=steeltoe;Pwd=steeltoe"
}
}
}
}
}
Initialize Steeltoe Connector
Update your Program.cs
as below to initialize the Connector:
using Steeltoe.Connectors.MySql;
var builder = WebApplication.CreateBuilder(args);
builder.AddMySql();
Use MySqlConnection
To obtain a MySqlConnection
instance in your application, inject the Steeltoe factory in a controller or view:
using Microsoft.AspNetCore.Mvc;
using MySqlConnector;
using Steeltoe.Connectors;
using Steeltoe.Connectors.MySql;
public class HomeController : Controller
{
public async Task<IActionResult> Index(
[FromServices] ConnectorFactory<MySqlOptions, MySqlConnection> connectorFactory)
{
var connector = connectorFactory.Get();
await using MySqlConnection connection = connector.GetConnection();
await connection.OpenAsync();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT 1";
object? result = await command.ExecuteScalarAsync();
ViewData["Result"] = result;
return View();
}
}
A complete sample app that uses MySqlConnection
is provided at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/MySql.
Use Entity Framework Core
Start by defining your DbContext
class:
public class AppDbContext : DbContext
{
public DbSet<SampleEntity> SampleEntities => Set<SampleEntity>();
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
}
public class SampleEntity
{
public long Id { get; set; }
public string? Text { get; set; }
}
Next, call the UseMySql()
Steeltoe extension method from Program.cs
to initialize Entity Framework Core:
using Steeltoe.Connectors.EntityFrameworkCore.MySql;
using Steeltoe.Connectors.MySql;
var builder = WebApplication.CreateBuilder(args);
builder.AddMySql();
builder.Services.AddDbContext<AppDbContext>(
(serviceProvider, options) => options.UseMySql(serviceProvider));
Once you have configured and added your DbContext
to the service container,
you can inject it and use it in a controller or view:
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
public class HomeController : Controller
{
public async Task<IActionResult> Index([FromServices] AppDbContext appDbContext)
{
List<SampleEntity> entities = await appDbContext.SampleEntities.ToListAsync();
return View(entities);
}
}
A complete sample app that uses Entity Framework Core with MySQL is provided at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/MySqlEFCore.
Cloud Foundry
This Connector supports the following service brokers:
- VMware SQL with MySQL for Tanzu Application Service
- VMware Tanzu Cloud Service Broker for Azure
- VMware Tanzu Cloud Service Broker for GCP
You can create and bind an instance to your application by using the Cloud Foundry CLI:
# Create MySQL service
cf create-service p.mysql db-small myMySqlService
# Bind service to your app
cf bind-service myApp myMySqlService
# Restage the app to pick up change
cf restage myApp
Kubernetes
This Connector supports the Service Binding Specification for Kubernetes. It can be used through the Bitnami Services Toolkit.
For details on how to use this, see the instructions at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/MySql#running-on-tanzu-application-platform-tap.