Contribute to Docs Contribute to Docs

MySQL

Please remember that if you are going to use our provided packages for ASP.NET Core Identity you will also need to include the MySQL package for Identity. For more information, please refer to the article about using Identity for Piranha.

Installation

To use MySQL you need to add the NuGet package for running Piranha with Entity Framework Core and MySQL.

PM> install-package Piranha.Data.EF.MySql

Optional

PM> install-package Piranha.AspNetCore.Identity.MySQL

Registering The DbContext

After you made sure you have the package reference you need you simply specify that you want to use MySQL and the connection information you want for your database in your Startup.cs. It is good practice to specify your connection string in your appsettings.json so you can have different connection strings for different environments, such as,

{
    "ConnectionStrings": {
        "mydb": "server=<THE-HOST-URL>;port=<THE-PORT>;database=<THE-DATABASE-SCHEME>;uid=<THE-DATABASE-USER>;password=<THE-PASSWORD>"
    }
}

Once the configuration is set up it can be used in the ConfigureServices of the Startup.cs as shown below.

public class Startup
{
    public IConfiguration Configuration { get; private set; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddPiranha(options =>
        {
            ...

            var connString = Configuration.GetConnectionString("mydb");

            options.UseEF<Piranha.Data.EF.MySql.MySqlDb>(o =>
            {
                o.UseMySql(connString);
            });

            // Optional
            // First add nuget package Piranha.AspNetCore.Identity.MySQL
            options.UseIdentityWithSeed<Piranha.AspNetCore.Identity.MySQL.IdentityMySQLDb>(db => db.UseMySql(connString));

            ...

        });
    }
}

As of Piranha Version 10.0, the MySQL package introduces a breaking change on how the database is configured. In addition to the connection string, the used database version needs to be specified. This can be accomplished by setting the specific version, for exmple

var connString = Configuration.GetConnectionString("mydb");
var serverVersion = new MySqlServerVersion(new Version(8, 0, 21));

options.UseEF<Piranha.Data.EF.MySql.MySqlDb>(o =>
{
    o.UseMySql(connString, serverVersion);
});

or by using the build-in auto detection feature based on the given connections string, such as,

var connString = Configuration.GetConnectionString("mydb");

options.UseEF<Piranha.Data.EF.MySql.MySqlDb>(o =>
{
    o.UseMySql(connString, ServerVersion.AutoDetect(connString));
});

That's it! You're now using MySQL with Piranha.

Configuration Options

The used MySQL package provides a number of configuration options, such as, character options, server versions etc. which are described in detail in the package Wiki.