public void SeedClientsDataToContextIfEmpty(IClientsConfigData clientsConfig) { // Damned hack for ConfigurationDbContextFactory! DbConnectionSwitcher.SingleDbConnectionKey = "Ids4.TargetDbConnection"; using (ConfigurationDbContext context = new ConfigurationDbContextFactory(_configuration) .CreateDbContext(Array.Empty <string>())) { // Clients if (!context.Clients.Any()) { Console.WriteLine("Clients being populated"); foreach (var client in clientsConfig.GetClients().ToList()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } else { Console.WriteLine("Clients already populated"); } // IdentityResources if (!context.IdentityResources.Any()) { Console.WriteLine("IdentityResources being populated"); foreach (var resource in clientsConfig.GetIdentityResources().ToList()) { context.IdentityResources.Add(resource.ToEntity()); } context.SaveChanges(); } else { Console.WriteLine("IdentityResources already populated"); } // ApiResources if (!context.ApiResources.Any()) { Console.WriteLine("ApiResources being populated"); foreach (var resource in clientsConfig.GetApiResources().ToList()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } else { Console.WriteLine("ApiResources already populated"); } } }
public void SeedAdminClient(IClientsConfigData clientsConfig) { // Damned hack for ConfigurationDbContextFactory! DbConnectionSwitcher.SingleDbConnectionKey = "Ids4.TargetDbConnection"; using (ConfigurationDbContext ctx = new ConfigurationDbContextFactory(_configuration) .CreateDbContext(Array.Empty <string>())) { var adminClient = clientsConfig.GetClients() .First(x => x.ClientName == AdminPortalConsts.OidcClientId); ctx.Clients.Add(adminClient.ToEntity()); ctx.SaveChanges(); } }