public async Task <bool> SaveChangesAsync() { return(await _context.SaveChangesAsync() > 0); }
private async Task Seed(IconicFundDbContext context, IWebHostEnvironment _hostingEnvironment, IHasherService hasherService) { if (!await context.Roles.AnyAsync()) { context.Roles.Add(new Role { Id = Guid.Parse(Constants.MainAdminRoleId), Name = "مدير النظام" }); //context.Roles.Add(new Role { Id = Guid.Parse(Constants.SaftyRoleId), Name = "السلامة" }); //context.Roles.Add(new Role { Id = Guid.Parse(Constants.LiftsRoleId), Name = "المصاعد" }); //context.Roles.Add(new Role { Id = Guid.Parse(Constants.EngineeringOfficeRoleId), Name = "المكاتب الهندسية" }); await context.SaveChangesAsync(); } if (!await context.Admins.AnyAsync()) { context.Admins.Add(new Admin { Id = Guid.Parse(Constants.MainAdminId), FirstName = "مدير", LastName = "النظام", MobileNumber = "567123432", Email = "*****@*****.**", EmplyeeNo = "1", NationalId = "1", Password = hasherService.ComputeSha256Hash("mainadmin"), CanApprove = true, IsActive = true, ActivationStartDate = DateTime.Now, ActivationEndDate = null, Roles = new List <AdminRole> { new AdminRole { RoleId = Guid.Parse(Constants.MainAdminRoleId) } } }); await context.SaveChangesAsync(); } //if (!context.SaftyOffices.Any()) //{ // context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالمنطقة المركزية " }); // context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالشوقية" }); // context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالعزيزية" }); // context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالمعابدة" }); // context.SaftyOffices.Add(new SaftyOffice { Name = "مركز السلامة الميدانية بالشرايع" }); // await context.SaveChangesAsync(); //} if (!context.Nationalities.Any() && !context.Cities.Any() && !context.Regions.Any()) { var seedPath = Path.Combine(_hostingEnvironment.ContentRootPath, "Seed"); var CitiesJSONtxt = File.ReadAllText(Path.Combine(seedPath, "Cities.json")); var NationalitiesJSONtxt = File.ReadAllText(Path.Combine(seedPath, "Nationalities.json")); var RegionsJSONtxt = File.ReadAllText(Path.Combine(seedPath, "Regions.json")); var Cities = JsonConvert.DeserializeObject <CityViewModels>(CitiesJSONtxt); var Nationalities = JsonConvert.DeserializeObject <NationalitiesViewModel>(NationalitiesJSONtxt); var Regions = JsonConvert.DeserializeObject <RegionsViewModel>(RegionsJSONtxt); context.Database.OpenConnection(); try { await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Cities ON"); context.Cities.AddRange(Cities.Cities); await context.SaveChangesAsync(); await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Cities OFF"); await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Regions ON"); context.Regions.AddRange(Regions.Regions); await context.SaveChangesAsync(); await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Regions OFF"); await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Nationalities ON"); context.Nationalities.AddRange(Nationalities.Nationalities); await context.SaveChangesAsync(); await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT dbo.Nationalities OFF"); } finally { context.Database.CloseConnection(); } //context.Cities.AddRange(Cities.Cities); //context.Nationalities.AddRange(Nationalities.Nationalities); //context.Regions.AddRange(Regions.Regions); //await context.SaveChangesAsync(); } }