Skip to content

Setup the custom database feature

Jon P Smith edited this page May 4, 2024 · 4 revisions

AuthP version 5.0.0 contains the custom database feature, which allows to use a larger of database types than the build-in SqlServer and PostgreSQL database types.

BE WARNED: Using a database provider that isn't build-in isn't as simple as calling one method and it all works. You need to build some AuthP setup code and create new migrations. The good thing is there are examples in the AuthP.CustomDatabaseExamples repo.

The supported databases are:

Supported database providers in V5.0.0 Comment
Microsoft.EntityFrameworkCore.SqlServer Built-in
Npgsql.EntityFrameworkCore.PostgreSQL Built-in
Microsoft.EntityFrameworkCore.Sqlite See examples in AuthP.CustomDatabaseExamples repo
Microsoft.EntityFrameworkCore.Cosmos
Pomelo.EntityFrameworkCore.MySql Pomelo Foundation Project
MySql.EntityFrameworkCore MySQL project
Oracle.EntityFrameworkCore Oracle

NOTE: The AuthP library uses Giorgi Dalakishvili’s EntityFramework.Exceptions library to detect concurrency and unique errors, and Giorgi only supports the main EF Core database providers, i.e. SqlServer, PostgreSQL, SQLite, MySQL, MySQL.Pomelo, and Oracle.

An overview of stages to use custom database with AuthP

You need to:

  1. Create EF Core migration(s) for your custom database
  2. Create an extension method to register your custom database to AuthP:
  3. Optional: Change any tenant data to your new custom database

The three stages are the same for any type of AuthP application, but there are different parts with sharding being the most complex.

  • A non-tenant application (e.g you are only using its Roles/Permissions feature) is has apply steps 1 and 2
  • A multi-tenant application needs to apply all three steps, but if you are using sharding then you have more options to decide on.

1. Create EF Core migrations

Each database type needs different commands to create or update a database - this is known as a migration. AuthP uses EF Core, which has various ways to create a EF migration. In my code I use EF Core IDesignTimeDbContextFactory approach and I will provide examples using this approach

There are 1 or 2 parts that you need a migration for.

1. AuthPermissionsDbContext

You always need to create a migration for AuthP's AuthPermissionsDbContext DbContext when not using the build-in database types.

The AuthP AuthPermissionsDbContext contains entries to administration of your application. The AuthPermissionsDbContext contains classes holding the various data, such as AuthUser, UserToRole, etc. to hold the data, but also has extra code in the AuthPermissionsDbContext's OnModelCreating method that sets up concurrency tokens. This is by done by code as concurrency tokens work differently with each database type.

This means you need to provide the extra concurrency token code that works for your custom database type, which is injected into the AuthPermissionsDbContext constructor. The AuthP.CustomDatabaseExamples has an example of creating a Sqlite migration in AuthP.CustomDatabaseExamples AuthPermissionsDbContextSqlite.

2. The tenant database(s)

If you are building a multi-tenant application, then you always need to create a migration for the tenant database. You have needed to this before version 5.0.0 anyway, and there is lots of examples in Example3, Example4 and Example6 of the AuthPermissions.AspNetCore repo and CustomDatabase1 and CustomDatabase2 in AuthP.CustomDatabaseExamples repo.

2. Create an AuthP extension method

There are two situations, depending whether you are using sharding in a multi-tenant application

2.1 Create an AuthP extension method for normal applications

You need to create an extension method to register your custom database. You do this by copying one of existing extension methods already in the AuthP code, such as UsingEfCoreSqlServer, and alter six parts:

  1. You set the AuthPDatabaseType to the enum AuthPDatabaseTypes.CustomDatabase
  2. Change the AuthPermissionsDbContext to your custom database provider.
  3. Link to your assembly containing the AuthPermissionsDbContext migration.
  4. Update the EntityFramework.Exceptions to your custom database provider.
  5. Add new code to register custom database configuration code.
  6. Optional: Update the RunMethodsSequentially code to provide a global lock

See the example in AuthP.CustomDatabaseExamples CustomDatabase1.SqliteCustomParts UsingEfCoreSqlite extension method.

2.2 Create an AuthP extension method for applications using sharding

Changing the database type for sharding follows the same approach at non-sharding described above, but you create a new version of the SetupMultiTenantSharding and then you change parts of the to use a different database. See SetupMultiTenantShardingCustomDb for an example.

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally