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 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 as well as a MySQL provider for ASP.NET Core. It is recommend to use the package provided by the Pomelo Foundation, namely Pomelo.EntityFrameworkCore.MySql , as it is also used by the Piranha package for ASP.NET Core Identity for MySQL and will work nicely if you decide to use the manager and identity package later on.

To get started, simply install the following packages:

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

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.AddPiranhaEF(options =>
            options.UseMySql(Configuration.GetConnectionString("mydb"));

        ...
    }
}

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 optinos, server versions etc. which are described in detail in the package Wiki.