Ejemplo n.º 1
0
        public static async Task InitializeDatabaseAsync(IServiceProvider serviceProvider)
        {
            using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                // grab our db context from the IOC container
                var db = serviceScope.ServiceProvider.GetService <ApplicationDbContext>();

                // create db if needed
                await db.Database.EnsureCreatedAsync();

                if (!db.Blog.Any())
                {
                    db.Blog.AddRange(_blogs);
                    db.SaveChangesAsync().Wait();
                }

                if (!db.RiskClass.Any())
                {
                    db.RiskClass.AddRange(_riskClasses);
                    db.SaveChangesAsync().Wait();
                }

                if (!db.RiskCategory.Any())
                {
                    db.RiskCategory.AddRange(_riskCategories);
                    db.SaveChangesAsync().Wait();
                }

                if (!db.RiskItem.Any())
                {
                    db.RiskItem.AddRange(_riskItems);
                    db.SaveChangesAsync().Wait();
                }

                if (!db.RiskReport.Any())
                {
                    RiskReport report = new RiskReport()
                    {
                        Title = "DZ Bank New York: Standard Risk Rating",
                    };
                    RiskReport report2 = new RiskReport()
                    {
                        Title = "DZ Bank New York: FI Risk Rating",
                    };

                    db.RiskReport.Add(report);
                    db.RiskReport.Add(report2);

                    var items = db.RiskItem.ToList();

                    foreach (var i in items)
                    {
                        if (i.Id < 7)
                        {
                            report.AddRiskItem(i);  // RRRIs.Add(new RRRI { RiskReportId = report.Id, RiskItemId = i.Id });
                        }
                        if (i.Id > 6)
                        {
                            report2.AddRiskItem(i); //.RRRIs.Add(new RRRI { RiskReportId = report2.Id, RiskItemId = i.Id });
                        }
                    }
                    db.SaveChangesAsync().Wait();
                }
            }
            await Task.FromResult(0);
        }