11using IdentityServer4 . EntityFramework . DbContexts ;
22using IdentityServer4 . EntityFramework . Mappers ;
33using Microsoft . eShopOnContainers . Services . Identity . API . Configuration ;
4+ using Microsoft . EntityFrameworkCore ;
45using Microsoft . Extensions . Configuration ;
56using System . Collections . Generic ;
67using System . Linq ;
@@ -10,47 +11,36 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Data
1011{
1112 public class ConfigurationDbContextSeed
1213 {
13- public async Task SeedAsync ( ConfigurationDbContext context , IConfiguration configuration )
14+ public async Task SeedAsync ( ConfigurationDbContext context , IConfiguration configuration )
1415 {
15-
1616 //callbacks urls from config:
17- var clientUrls = new Dictionary < string , string > ( ) ;
18-
19- clientUrls . Add ( "Mvc" , configuration . GetValue < string > ( "MvcClient" ) ) ;
20- clientUrls . Add ( "Spa" , configuration . GetValue < string > ( "SpaClient" ) ) ;
21- clientUrls . Add ( "Xamarin" , configuration . GetValue < string > ( "XamarinCallback" ) ) ;
22- clientUrls . Add ( "LocationsApi" , configuration . GetValue < string > ( "LocationApiClient" ) ) ;
23- clientUrls . Add ( "MarketingApi" , configuration . GetValue < string > ( "MarketingApiClient" ) ) ;
24- clientUrls . Add ( "BasketApi" , configuration . GetValue < string > ( "BasketApiClient" ) ) ;
25- clientUrls . Add ( "OrderingApi" , configuration . GetValue < string > ( "OrderingApiClient" ) ) ;
17+ var clientUrls = new Dictionary < string , string >
18+ {
19+ { "Mvc" , configuration . GetValue < string > ( "MvcClient" ) } ,
20+ { "Spa" , configuration . GetValue < string > ( "SpaClient" ) } ,
21+ { "Xamarin" , configuration . GetValue < string > ( "XamarinCallback" ) } ,
22+ { "LocationsApi" , configuration . GetValue < string > ( "LocationApiClient" ) } ,
23+ { "MarketingApi" , configuration . GetValue < string > ( "MarketingApiClient" ) } ,
24+ { "BasketApi" , configuration . GetValue < string > ( "BasketApiClient" ) } ,
25+ { "OrderingApi" , configuration . GetValue < string > ( "OrderingApiClient" ) }
26+ } ;
2627
27- if ( ! context . Clients . Any ( ) )
28+ if ( ! await context . Clients . AnyAsync ( ) )
2829 {
29- foreach ( var client in Config . GetClients ( clientUrls ) )
30- {
31- await context . Clients . AddAsync ( client . ToEntity ( ) ) ;
32- }
33- await context . SaveChangesAsync ( ) ;
30+ context . Clients . AddRange ( Config . GetClients ( clientUrls ) . Select ( client => client . ToEntity ( ) ) ) ;
3431 }
3532
36- if ( ! context . IdentityResources . Any ( ) )
33+ if ( ! await context . IdentityResources . AnyAsync ( ) )
3734 {
38- foreach ( var resource in Config . GetResources ( ) )
39- {
40- await context . IdentityResources . AddAsync ( resource . ToEntity ( ) ) ;
41- }
42- await context . SaveChangesAsync ( ) ;
35+ context . IdentityResources . AddRange ( Config . GetResources ( ) . Select ( resource => resource . ToEntity ( ) ) ) ;
4336 }
4437
45- if ( ! context . ApiResources . Any ( ) )
38+ if ( ! await context . ApiResources . AnyAsync ( ) )
4639 {
47- foreach ( var api in Config . GetApis ( ) )
48- {
49- await context . ApiResources . AddAsync ( api . ToEntity ( ) ) ;
50- }
51-
52- await context . SaveChangesAsync ( ) ;
40+ context . ApiResources . AddRange ( Config . GetApis ( ) . Select ( api => api . ToEntity ( ) ) ) ;
5341 }
42+
43+ await context . SaveChangesAsync ( ) ;
5444 }
5545 }
5646}
0 commit comments