public async Task <IHttpActionResult> PostCustomer(Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } customer.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(customer); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException) { if (_dbContext.Customers.Any(c => c.CustomerId == customer.CustomerId)) { return(Conflict()); } throw; } await _dbContext.LoadRelatedEntitiesAsync(customer); customer.AcceptChanges(); return(CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer)); }
public async Task <IActionResult> PutOrder([FromBody] Order order) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Apply changes to context _context.ApplyChanges(order); try { // Persist changes await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_context.Orders.Any(o => o.OrderId == order.OrderId)) { return(NotFound()); } throw; } // Populate reference properties await _context.LoadRelatedEntitiesAsync(order); // Reset tracking state to unchanged _context.AcceptChanges(order); //return NoContent(); return(Ok(order)); }
public async Task <IHttpActionResult> PostProduct(Product entity) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } entity.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(entity); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException) { if (_dbContext.Products.Any(e => e.ProductId == entity.ProductId)) { return(Conflict()); } throw; } await _dbContext.LoadRelatedEntitiesAsync(entity); entity.AcceptChanges(); return(CreatedAtRoute("DefaultApi", new { id = entity.ProductId }, entity)); }
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)); }
public async Task <IHttpActionResult> PostOrder(Order order) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } order.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(order); await _dbContext.SaveChangesAsync(); await _dbContext.LoadRelatedEntitiesAsync(order); order.AcceptChanges(); return(CreatedAtRoute("DefaultApi", new { id = order.OrderId }, order)); }
public async Task <IHttpActionResult> PostEmployee(Employee employee) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } employee.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(employee); await _dbContext.SaveChangesAsync(); await _dbContext.LoadRelatedEntitiesAsync(employee); employee.AcceptChanges(); return(CreatedAtRoute("DefaultApi", new { id = employee.EmployeeId }, employee)); }
public async Task <Customer> CreateCustomer(Customer customer) { customer.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(customer); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException updateEx) { throw new FaultException(updateEx.Message); } await _dbContext.LoadRelatedEntitiesAsync(customer); customer.AcceptChanges(); return(customer); }
public async Task <Order> CreateOrder(Order order) { // Mark order as added order.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(order); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException updateEx) { throw new FaultException(updateEx.Message); } // Load related entities and accept changes await _dbContext.LoadRelatedEntitiesAsync(order); order.AcceptChanges(); return(order); }