Table of Contents

MySQL

This connector simplifies accessing MySQL databases. It supports the following .NET drivers:

The remainder of this topic assumes that you are familiar with the basic concepts of Steeltoe Connectors. See Overview for more information.

Using the MySQL connector

To use this connector:

  1. Create a MySQL server instance or use a docker container.
  2. Add NuGet references to your project.
  3. Configure your connection string in appsettings.json.
  4. Initialize the Steeltoe Connector at startup.
  5. 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

For the available connection string parameters for MySQL, see:

The following example appsettings.json uses the docker container referred to earlier:

{
  "Steeltoe": {
    "Client": {
      "MySql": {
        "Default": {
          "ConnectionString": "Server=localhost;Database=steeltoe;Uid=steeltoe;Pwd=steeltoe"
        }
      }
    }
  }
}

Initialize Steeltoe Connector

Update your Program.cs as shown here 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

To retrieve data from MySQL in your app using Entity Framework Core, use the following steps:

  1. Define 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; }
    }
    
  2. 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));
    
  3. After 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:

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 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-platform-for-kubernetes.