Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
        public ITestActionResult Get(int key)
        {
            ForeignKeyCustomer customer = _db.Customers.FirstOrDefault(c => c.Id == key);

            if (customer == null)
            {
                return(NotFound());
            }

            return(Ok(customer));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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));
        }