public IActionResult Campgrounds() { APIHandler webHandler = new APIHandler(); RootObject dat = webHandler.GetCampgrounds(); foreach (Datum data in dat.data) { //Database will give PK constraint violation error when trying to insert record with existing PK. //So add company only if it doesnt exist, check existence using symbol (PK) if (dbContext.RootObject.Where(p => p.parkCode.Equals(data.parkCode)).Count() == 0) { dbContext.RootObject.Add(data); } } dbContext.SaveChanges(); return(View(dat)); }