public IHttpActionResult PostCustomer(CustomerModel customer) { //If everything is good with the communication if (!ModelState.IsValid) { return BadRequest(ModelState); } //Build new Customer var dbCustomer = new Customer(); //Update Customer with new Model dbCustomer.Update(customer); //add Customer model to DB db.Customers.Add(dbCustomer); db.SaveChanges(); customer.CustomerId = dbCustomer.CustomerId; return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer); }
public IHttpActionResult PostCustomer(CustomerModel customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var dbPostCustomer = new Customer(); dbPostCustomer.Update(customer); db.Customers.Add(dbPostCustomer); db.SaveChanges(); customer.CustomerID = dbPostCustomer.CustomerID; return CreatedAtRoute("DefaultApi", new { id = dbPostCustomer.CustomerID }, customer); }
public IHttpActionResult PostCustomer(CustomerModel customer) { // Validate the request if (!ModelState.IsValid) { return BadRequest(ModelState); } // Set up new Customer object, // and populate it with the values from // the input CustomerModel object Customer dbCustomer = new Customer(); dbCustomer.Update(customer); // Add the new Customer object to the list of Customer objects db.Customers.Add(dbCustomer); // Save the changes to the DB try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the customer to the database."); } // Update the CustomerModel object with the new customer ID // that was placed in the Customer object after the changes // were saved to the DB customer.CustomerId = dbCustomer.CustomerId; return CreatedAtRoute("DefaultApi", new { id = dbCustomer.CustomerId }, customer); }