public virtual void Update(T entity) { try { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } _context.Set <T>().Update(entity); _context.SaveChanges(); } catch (DbEntityValidationException dbEx) { throw new Exception(dbEx.Message); } }
// Use at PopulationChart.cshtml >LoadData >url: "/Home/PopulationData" public JsonResult PopulationData() { using (var context = new PopulationDbContext()) { // Clear and reset database context.Database.EnsureDeleted(); // Create database if it does not exist context.Database.EnsureCreated(); // Add records: Id is autoincremented by default context.PopulationDbSet.Add(new PopulationModel { CityName = "City-1", PopulationYear2010 = 3000, PopulationYear2000 = 1000 }); context.PopulationDbSet.Add(new PopulationModel { CityName = "City-2", PopulationYear2010 = 2500, PopulationYear2000 = 1500 }); context.PopulationDbSet.Add(new PopulationModel { CityName = "City-3", PopulationYear2010 = 2000, PopulationYear2000 = 2000 }); context.PopulationDbSet.Add(new PopulationModel { CityName = "City-4", PopulationYear2010 = 1500, PopulationYear2000 = 2500 }); context.PopulationDbSet.Add(new PopulationModel { CityName = "City-5", PopulationYear2010 = 1000, PopulationYear2000 = 3000 }); // Commit changes context.SaveChanges(); // Fetch all Population var list = new List <PopulationModel>(); foreach (var population in context.PopulationDbSet.ToList()) { list.Add(new PopulationModel { CityName = population.CityName, PopulationYear2010 = population.PopulationYear2010, PopulationYear2000 = population.PopulationYear2000 }); } return(Json(list)); } }