public async Task<IHttpActionResult> PutTerritory(Territory territory) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _dbContext.ApplyChanges(territory); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_dbContext.Territories.Any(t => t.TerritoryId == territory.TerritoryId)) { return Conflict(); } throw; } await _dbContext.LoadRelatedEntitiesAsync(territory); territory.AcceptChanges(); return Ok(territory); }
public async Task<IHttpActionResult> PostTerritory(Territory territory) { if (!ModelState.IsValid) { return BadRequest(ModelState); } territory.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(territory); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException) { if (_dbContext.Territories.Any(t => t.TerritoryId == territory.TerritoryId)) { return Conflict(); } throw; } await _dbContext.LoadRelatedEntitiesAsync(territory); territory.AcceptChanges(); return CreatedAtRoute("DefaultApi", new { id = territory.TerritoryId }, territory); }