Contribute to Docs Contribute to Docs

SQL Server

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

Whether you're using SQL Express, SQL Server or SQL Azure it's the same setup. SQL Server is a high performance database intended for production usage that can handle very high workloads.

Installation

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

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

Registering The DbContext

After you've made sure you have the package reference you need you simply specify that you want to use SQL Server and the connection information you want for your database in your Startup.cs. Add a new file appsettings.json to the root of your application. It's good practice to specify your connection string in your appsettings.json so you can have different connection strings for different environments.

{
    "ConnectionStrings": {
        "piranha": "Data Source=;Initial Catalog=;User Id=;Password=;MultipleActiveResultSets=True"
    }
}

When you've setup your configuration string you can access it from ConfigureServices in your Startup.cs.

using Piranha.Data.EF.SQLServer;
using Microsoft.Extensions.Configuration;

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

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

    public void ConfigureServices(IServiceCollection services)
    {
        ...

        // Service setup
        services.AddPiranha(options =>
        {
            options.UseEF<SQLServerDb>(db =>
                db.UseSqlServer(Configuration.GetConnectionString("piranha")));
        });

        ...
    }
}

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