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<IHttpActionResult> PutCustomer(Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _dbContext.ApplyChanges(customer); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_dbContext.Customers.Any(c => c.CustomerId == customer.CustomerId)) { return Conflict(); } throw; } await _dbContext.LoadRelatedEntitiesAsync(customer); customer.AcceptChanges(); return Ok(customer); }
private static void EnsureTestCustomer(NorthwindDbContext context, string customerId, string territoryId) { var customer = context.Customers .SingleOrDefault(c => c.CustomerId == customerId); if (customer == null) { customer = new Customer { CustomerId = customerId, CustomerName = "Test Customer " + customerId, TerritoryId = territoryId }; context.Customers.Add(customer); } }
public static void EnsureTestCustomer(string customerId, string customerName) { using (var context = CreateNorthwindDbContext(CreateNorthwindDbOptions)) { var customer = context.Customers .SingleOrDefault(c => c.CustomerId == customerId); if (customer == null) { customer = new Customer { CustomerId = customerId, CustomerName = customerName }; context.Customers.Add(customer); context.SaveChanges(); } } }