-
Notifications
You must be signed in to change notification settings - Fork 161
Setup the custom database feature
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.
You need to:
- Create EF Core migration(s) for your custom database
- Create an extension method to register your custom database to AuthP:
- 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.
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.
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
.
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.
There are two situations, depending whether you are using sharding in a multi-tenant application
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:
- You set the
AuthPDatabaseType
to the enumAuthPDatabaseTypes.CustomDatabase
- Change the AuthPermissionsDbContext to your custom database provider.
- Link to your assembly containing the AuthPermissionsDbContext migration.
- Update the EntityFramework.Exceptions to your custom database provider.
- Add new code to register custom database configuration code.
- Optional: Update the
RunMethodsSequentially
code to provide a global lock
See the example in AuthP.CustomDatabaseExamples
CustomDatabase1.SqliteCustomParts UsingEfCoreSqlite
extension method.
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.
- Intro to multi-tenants (ASP.NET video)
- Articles in date order:
- 0. Improved Roles/Permissions
- 1. Setting up the database
- 2. Admin: adding users and tenants
- 3. Versioning your app
- 4. Hierarchical multi-tenant
- 5. Advanced technique with claims
- 6. Sharding multi-tenant setup
- 7. Three ways to add new users
- 8. The design of the sharding data
- 9. Down for maintenance article
- 10: Three ways to refresh claims
- 11. Features of Multilingual service
- 12. Custom databases - Part1
- Videos (old)
- Authentication explained
- Permissions explained
- Roles explained
- AuthUser explained
- Multi tenant explained
- Sharding explained
- How AuthP handles sharding
- How AuthP handles errors
- Languages & cultures explained
- JWT Token refresh explained
- Setup Permissions
- Setup Authentication
- Startup code
- Setup the custom database feature
- JWT Token configuration
- Multi tenant configuration
- Using Permissions
- Using JWT Tokens
- Creating a multi-tenant app
- Supporting multiple languages
- Unit Test your AuthP app