示例#1
0
        private void InitializeDatabase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                Console.WriteLine("开始初始化 PersistedGrantDbContext ...");
                #region PersistedGrantDbContext

                serviceScope.ServiceProvider.GetRequiredService <PersistedGrantCustomDbContext>().Database.Migrate();

                #endregion

                Console.WriteLine("开始初始化 ConfigurationDbContext ...");
                #region ConfigurationDbContext

                var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationCustomDbContext>();
                context.Database.Migrate();

                Console.WriteLine("开始初始化 Clients ...");
                if (!context.Clients.Any())
                {
                    foreach (var client in DataConfiguration.GetClients())
                    {
                        context.Clients.Add(client.ToEntity());
                    }
                    context.SaveChanges();
                }

                Console.WriteLine("开始初始化 IdentityResources ...");
                if (!context.IdentityResources.Any())
                {
                    foreach (var resource in DataConfiguration.GetIdentityResources())
                    {
                        context.IdentityResources.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }

                Console.WriteLine("开始初始化 ApiResources ...");
                if (!context.ApiResources.Any())
                {
                    foreach (var resource in DataConfiguration.GetApiResources())
                    {
                        context.ApiResources.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }

                #endregion
            }
        }