SQL Server

Last Updated: 2019-02-03

Please remember that if you're going to use our provided packages for AspNetCore Identity you will need need to include Piranha.AspNetCore.Identity.SQLServer. For more information, please refer to Identity Security.

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, make sure you add the NuGet package for running Entity Framework Core with SQL Server.

Microsoft.EntityFrameworkCore.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. It's good practice to specify your connection string in your appsettings.json so you can have different connection strings for different environments.

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

When you've setup your configuration string you can then access it from your ConfigureServices.

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

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public void ConfigureServices(IServiceCollection services)
{
...

services.AddPiranhaEF(options =>
options.UseSqlServer(Configuration.GetConnectionString("mydb"));

...
}
}

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