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));
        }
        // POST api/Orders
        public HttpResponseMessage PostOrder(Order order)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            order.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(order);

            try
            {
                _dbContext.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                if (_dbContext.Orders.Any(o => o.OrderId == order.OrderId))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex));
                }
                throw;
            }

            _dbContext.LoadRelatedEntities(order);
            order.AcceptChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, order);

            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = order.OrderId }));
            return(response);
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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));
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        public void ApplyChanges_Should_Mark_Entity_As_Added()
        {
            using (var context = new NorthwindSlimContext(_options))
            {
                // Arrange
                var product = new Product
                {
                    ProductId   = 1,
                    ProductName = "Chai",
                    UnitPrice   = 10
                };
                product.TrackingState = TrackingState.Added;

                // Act
                context.ApplyChanges(product);

                // Assert
                Assert.Equal(EntityState.Added, context.Entry(product).State);
            }
        }