public async Task <Customer> UpdateCustomerAsync(Customer customer)
        {
            using (var context = new CustomersServiceDbContext())
            {
                var cust = await context.Customers.FirstOrDefaultAsync(c => c.Id == customer.Id);

                if (cust != null)
                {
                    cust.FirstName            = customer.FirstName;
                    cust.LastName             = customer.LastName;
                    cust.Address              = customer.Address;
                    cust.Phone                = customer.Phone;
                    cust.Email                = customer.Email;
                    context.Entry(cust).State = EntityState.Modified;
                }
                else
                {
                    context.Customers.Attach(customer);
                }

                await context.SaveChangesAsync();
            }

            return(customer);
        }
Ejemplo n.º 2
0
        public async Task <Order> AddOrderAsync(Order order)
        {
            using (var context = new CustomersServiceDbContext())
            {
                context.Orders.Add(order);
                await context.SaveChangesAsync();
            }

            return(order);
        }
        public async Task <bool> AddCustomerAsync(Customer customer)
        {
            bool retValue = true;

            using (var context = new CustomersServiceDbContext())
            {
                context.Customers.Add(customer);
                retValue = await context.SaveChangesAsync() >= 1;
            }

            return(retValue);
        }
Ejemplo n.º 4
0
        public async Task <Order> UpdateOrderAsync(Order order)
        {
            using (var context = new CustomersServiceDbContext())
            {
                if (!context.Orders.Local.Any(o => o.Id == order.Id))
                {
                    context.Orders.Attach(order);
                }
                context.Entry(order).State = EntityState.Modified;
                await context.SaveChangesAsync();
            }

            return(order);
        }