/// <summary> /// Register DbContexts for IdentityServer ConfigurationStore and PersistedGrants, Identity and Logging /// Configure the connection strings in AppSettings.json /// </summary> /// <typeparam name="TConfigurationDbContext"></typeparam> /// <typeparam name="TPersistedGrantDbContext"></typeparam> /// <typeparam name="TLogDbContext"></typeparam> /// <typeparam name="TIdentityDbContext"></typeparam> /// <typeparam name="TAuditLoggingDbContext"></typeparam> /// <typeparam name="TDataProtectionDbContext"></typeparam> /// <typeparam name="TAuditLog"></typeparam> /// <param name="services"></param> /// <param name="connectionStrings"></param> /// <param name="databaseProvider"></param> /// <param name="databaseMigrations"></param> public static void RegisterDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>( this IServiceCollection services, ConnectionStringsConfiguration connectionStrings, DatabaseProviderConfiguration databaseProvider, DatabaseMigrationsConfiguration databaseMigrations) where TIdentityDbContext : DbContext where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext where TLogDbContext : DbContext, IAdminLogDbContext where TAuditLoggingDbContext : DbContext, IAuditLoggingDbContext <TAuditLog> where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext where TAuditLog : AuditLog { switch (databaseProvider.ProviderType) { case DatabaseProviderType.SqlServer: services.RegisterSqlServerDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(connectionStrings, databaseMigrations); break; case DatabaseProviderType.PostgreSQL: services.RegisterNpgSqlDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(connectionStrings, databaseMigrations); break; case DatabaseProviderType.MySql: services.RegisterMySqlDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(connectionStrings, databaseMigrations); break; default: throw new ArgumentOutOfRangeException(nameof(databaseProvider.ProviderType), $@"The value needs to be one of {string.Join(", ", Enum.GetNames(typeof(DatabaseProviderType)))}."); } }
/// <summary> /// Generate migrations before running this method, you can use these steps bellow: /// https://github.com/skoruba/IdentityServer4.Admin#ef-core--data-access /// </summary> /// <param name="host"></param> /// <param name="applyDbMigrationWithDataSeedFromProgramArguments"></param> /// <param name="seedConfiguration"></param> /// <param name="databaseMigrationsConfiguration"></param> public static async Task ApplyDbMigrationsWithDataSeedAsync <TIdentityServerDbContext, TIdentityDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLogDbContext, TDataProtectionDbContext, TUser, TRole>( IHost host, bool applyDbMigrationWithDataSeedFromProgramArguments, SeedConfiguration seedConfiguration, DatabaseMigrationsConfiguration databaseMigrationsConfiguration) where TIdentityServerDbContext : DbContext, IAdminConfigurationDbContext where TIdentityDbContext : DbContext where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext where TLogDbContext : DbContext, IAdminLogDbContext where TAuditLogDbContext : DbContext, IAuditLoggingDbContext <AuditLog> where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext where TUser : IdentityUser, new() where TRole : IdentityRole, new() { using (var serviceScope = host.Services.CreateScope()) { var services = serviceScope.ServiceProvider; if ((databaseMigrationsConfiguration != null && databaseMigrationsConfiguration.ApplyDatabaseMigrations) || (applyDbMigrationWithDataSeedFromProgramArguments)) { await EnsureDatabasesMigratedAsync <TIdentityDbContext, TIdentityServerDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLogDbContext, TDataProtectionDbContext>(services); } if ((seedConfiguration != null && seedConfiguration.ApplySeed) || (applyDbMigrationWithDataSeedFromProgramArguments)) { await EnsureSeedDataAsync <TIdentityServerDbContext, TUser, TRole>(services); } } }
/// <summary> /// Register DbContexts for IdentityServer ConfigurationStore and PersistedGrants, Identity and Logging /// Configure the connection strings in AppSettings.json /// </summary> /// <typeparam name="TConfigurationDbContext"></typeparam> /// <typeparam name="TPersistedGrantDbContext"></typeparam> /// <typeparam name="TLogDbContext"></typeparam> /// <typeparam name="TIdentityDbContext"></typeparam> /// <typeparam name="TAuditLoggingDbContext"></typeparam> /// <param name="services"></param> /// <param name="connectionStrings"></param> /// <param name="databaseMigrations"></param> public static void RegisterNpgSqlDbContexts <TIdentityDbContext, TConfigurationDbContext, TPersistedGrantDbContext, TLogDbContext, TAuditLoggingDbContext, TDataProtectionDbContext, TAuditLog>(this IServiceCollection services, ConnectionStringsConfiguration connectionStrings, DatabaseMigrationsConfiguration databaseMigrations) where TIdentityDbContext : DbContext where TPersistedGrantDbContext : DbContext, IAdminPersistedGrantDbContext where TConfigurationDbContext : DbContext, IAdminConfigurationDbContext where TLogDbContext : DbContext, IAdminLogDbContext where TAuditLoggingDbContext : DbContext, IAuditLoggingDbContext <TAuditLog> where TDataProtectionDbContext : DbContext, IDataProtectionKeyContext where TAuditLog : AuditLog { var migrationsAssembly = typeof(DatabaseExtensions).GetTypeInfo().Assembly.GetName().Name; // Config DB for identity services.AddDbContext <TIdentityDbContext>(options => options.UseNpgsql(connectionStrings.IdentityDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.IdentityDbMigrationsAssembly ?? migrationsAssembly))); // Config DB from existing connection services.AddConfigurationDbContext <TConfigurationDbContext>(options => options.ConfigureDbContext = b => b.UseNpgsql(connectionStrings.ConfigurationDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.ConfigurationDbMigrationsAssembly ?? migrationsAssembly))); // Operational DB from existing connection services.AddOperationalDbContext <TPersistedGrantDbContext>(options => options.ConfigureDbContext = b => b.UseNpgsql(connectionStrings.PersistedGrantDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.PersistedGrantDbMigrationsAssembly ?? migrationsAssembly))); // Log DB from existing connection services.AddDbContext <TLogDbContext>(options => options.UseNpgsql(connectionStrings.AdminLogDbConnection, optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.AdminLogDbMigrationsAssembly ?? migrationsAssembly))); // Audit logging connection services.AddDbContext <TAuditLoggingDbContext>(options => options.UseNpgsql(connectionStrings.AdminAuditLogDbConnection, optionsSql => optionsSql.MigrationsAssembly(databaseMigrations.AdminAuditLogDbMigrationsAssembly ?? migrationsAssembly))); // DataProtectionKey DB from existing connection if (!string.IsNullOrEmpty(connectionStrings.DataProtectionDbConnection)) { services.AddDbContext <TDataProtectionDbContext>(options => options.UseNpgsql(connectionStrings.DataProtectionDbConnection, sql => sql.MigrationsAssembly(databaseMigrations.DataProtectionDbMigrationsAssembly ?? migrationsAssembly))); } }