/**** * Returns the Colleges List from API. ****/ public Schools GetSchoolsDatafromAPI() { APIHandler webHandler = new APIHandler(); Schools schoolsdata = webHandler.GetSchoolsDataAPI(); foreach (School school in schoolsdata.results) { //Database will give PK constraint violation error when trying to insert record with existing PK. //So add School only if it doesnt exist, check existence using symbol (PK) if (dbContext.Schools.Where(c => c.id.Equals(school.id)).Count() == 0) { dbContext.Schools.Add(school); } } dbContext.SaveChanges(); return(schoolsdata); }
public IActionResult CollegesList() { APIHandler webHandler = new APIHandler(); Schools schoolsdata = webHandler.GetSchoolsData(); foreach (School school in schoolsdata.results) { //Database will give PK constraint violation error when trying to insert record with existing PK. //So add School only if it doesnt exist, check existence using symbol (PK) //if ( (dbContext.Schools.Where(c => c.id.Equals(school.id)).Count() == 0) || (DateTime.Now.Date - Convert.ToDateTime(dbContext.Schools.Where(c => c.id.Equals(school.id))?.Select(c => c.CreatedOn.Date))).Days > 30 ) if (dbContext.Schools.Where(c => c.id.Equals(school.id)).Count() == 0) { dbContext.Schools.Add(school); } } dbContext.SaveChanges(); return View(schoolsdata); }