Database Configuration
Moka.Auth provides built-in support for multiple database providers and offers flexible configuration options for data storage and management.
Supported Providers
Moka.Auth supports the following database providers:
- SQLite - Lightweight file-based database, perfect for development and small applications
- PostgreSQL - Recommended for production applications with advanced features
- SQL Server - Enterprise-grade relational database for Microsoft-centric environments
- MySQL/MariaDB - Open-source relational database management system
Configuration Methods
Method 1: Using MokaAuthDbContext
The simplest approach, as shown in the Samples.JwtSqlite project:
// Configure Moka.Auth with SQLitebuilder.Services.AddMokaAuthDbContext(dbOptions =>{ dbOptions.ConnectionString = "Data Source=moka.db"; dbOptions.DbType = DbType.SQLite; dbOptions.AutoMigrate = true;});Method 2: Via appsettings.json
Define your database settings in appsettings.json:
{ "MokaAuth": { "Database": { "Provider": "SQLite", "ConnectionString": "Data Source=moka_auth.db", "AutoMigrate": true, "EnableSensitiveDataLogging": false } }}Then in your Program.cs, bind these settings:
// Option 1: Use binding from configurationbuilder.Services.AddMokaAuthDbContext(dbOptions =>{ builder.Configuration.GetSection("MokaAuth:Database").Bind(dbOptions);});Available Database Options
The DatabaseOptions class provides the following configuration:
| Option | Description | Default |
|---|---|---|
ConnectionString | Database connection string | "" |
DbType | Database provider type (SQLite, PostgreSQL, MySQL, SqlServer) | DbType.SQLite |
AutoMigrate | Automatically apply migrations on startup | true |
SchemaName | Schema name (for PostgreSQL) | null |
MaxRetryCount | Maximum number of retries for database operations | 5 |
MaxRetryDelaySeconds | Maximum retry delay in seconds | 30 |
EnableSensitiveDataLogging | Enable sensitive data logging (not recommended for production) | false |
EnableDetailedErrors | Enable detailed error messages | false |
CommandTimeoutSeconds | Command timeout in seconds | null |
Provider-Specific Examples
SQLite Example (from Samples.JwtSqlite)
builder.Services.AddMokaAuthDbContext(dbOptions =>{ dbOptions.ConnectionString = "Data Source=moka.db"; dbOptions.DbType = DbType.SQLite; dbOptions.AutoMigrate = true;});PostgreSQL Example
builder.Services.AddMokaAuthDbContext(dbOptions =>{ dbOptions.ConnectionString = "Host=localhost;Database=moka_auth;Username=user;Password=pass;"; dbOptions.DbType = DbType.PostgreSQL; dbOptions.SchemaName = "auth"; // Optional schema dbOptions.AutoMigrate = true;});SQL Server Example
builder.Services.AddMokaAuthDbContext(dbOptions =>{ dbOptions.ConnectionString = "Server=(localdb)\\mssqllocaldb;Database=MokaAuth;Trusted_Connection=True;MultipleActiveResultSets=true"; dbOptions.DbType = DbType.SqlServer; dbOptions.AutoMigrate = true;});MySQL Example
builder.Services.AddMokaAuthDbContext(dbOptions =>{ dbOptions.ConnectionString = "Server=localhost;Database=moka_auth;User=root;Password=password;"; dbOptions.DbType = DbType.MySQL; dbOptions.AutoMigrate = true;});Custom Database Context
If you need more control, you can use a custom database context:
// Using a custom database contextbuilder.Services.AddMokaAuthDbContext<CustomAuthDbContext>(dbOptions =>{ dbOptions.ConnectionString = "Data Source=moka.db"; dbOptions.DbType = DbType.SQLite; dbOptions.AutoMigrate = true;});Database Middleware
Don’t forget to add the database middleware to your application pipeline:
var app = builder.Build();
// ... other middleware
// Apply database configurationapp.UseMokaAuthDatabase();
// ... other middlewareData Seeding
Moka.Auth provides several methods to seed your database with initial data. See the Quick Start Guide for detailed examples.