private void Generate() { int orderId = 1; for (int i = 1; i <= 5; i++) { ForeignKeyCustomer customer = new ForeignKeyCustomer { Id = i, Name = "Customer #" + i, Orders = Enumerable.Range(1, 3).Select(e => new ForeignKeyOrder { OrderId = orderId, OrderName = "Order #" + orderId++, CustomerId = i }).ToList() }; foreach (var order in customer.Orders) { order.Customer = customer; } _db.Customers.Add(customer); _db.Orders.AddRange(customer.Orders); } _db.SaveChanges(); }
public ITestActionResult Get(int key) { ForeignKeyCustomer customer = _db.Customers.FirstOrDefault(c => c.Id == key); if (customer == null) { return(NotFound()); } return(Ok(customer)); }
public ITestActionResult Delete(int key) { ForeignKeyCustomer customer = _db.Customers.FirstOrDefault(c => c.Id == key); if (customer == null) { return(NotFound()); } _db.Customers.Remove(customer); _db.SaveChanges(); return(StatusCode(HttpStatusCode.NoContent)); }
public ITestActionResult Delete(int key) { ForeignKeyCustomer customer = _db.Customers.FirstOrDefault(c => c.Id == key); if (customer == null) { return(NotFound()); } try { _db.Customers.Remove(customer); _db.SaveChanges(); } catch (Exception e) { return(BadRequest(e.Message)); } return(StatusCode(HttpStatusCode.NoContent)); }