public async Task<IHttpActionResult> Put([FromODataUri] string key, Activity model)//[FromODataUri] string key, { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (key != model.Id) { return BadRequest(); } var category = await db.Activities.FindAsync(model.Id); if (category == null) { return NotFound(); } category.Modify(model, db); //db.Entry(category).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (Exception ex) { return BadRequest(ex.Message); } return Updated(model); }
public async Task<IHttpActionResult> Post(Activity model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { model.Create(db); db.Activities.Add(model); await db.SaveChangesAsync(); return Ok(model); } catch (Exception ex) { ModelState.AddModelError("PostError", ex.Message); return BadRequest(ModelState); } }
public void Modify(Activity model, ApplicationDbContext db) { base.Modify(model, db); CoverUrl = model.CoverUrl; BeginTime = model.BeginTime; EndTime = model.EndTime; Count = model.Count; model.Awards = model.Awards ?? new List<Award>(); var add = model.Awards.Where(o1 => !Awards.Any(o2 => o1.Id == o2.Id)).ToList(); var remove = Awards.Where(o1 => !model.Awards.Any(o2 => o1.Id == o2.Id)).ToList(); foreach (var item in add) { db.Entry(item).State = EntityState.Unchanged; Awards.Add(item); } foreach (var item in remove) { Awards.Remove(item); } }