Database Reference
In-Depth Information
default:
return EntityState.Unchanged;
}
}
Finally, replace the code in the CustomerController with that from Listing 9-25.
Listing 9-25. Customer Web API Controller
public class CustomerController : ApiController
{
// GET api/customer
public IEnumerable<Customer> Get()
{
using (var context = new Recipe4Context())
{
return context.Customers.Include(x => x.Phones).ToList();
}
}
// GET api/customer/5
public Customer Get(int id)
{
using (var context = new Recipe4Context())
{
return context.Customers.Include(x => x.Phones)
.FirstOrDefault(x => x.CustomerId == id);
}
}
[ActionName("Update")]
public HttpResponseMessage UpdateCustomer(Customer customer)
{
using (var context = new Recipe4Context())
{
// Add object graph to context setting default state of 'Added'.
// Adding parent to context automatically attaches entire graph
// (parent and child entities) to context and sets state to 'Added'
// for all entities.
context.Customers.Add(customer);
foreach (var entry in context.ChangeTracker.Entries<BaseEntity>())
{
entry.State = EntityStateFactory.Set(entry.Entity.TrackingState);
if (entry.State == EntityState.Modified)
{
// For entity updates, we fetch a current copy of the entity
// from the database and assign the values to the orginal values
// property from the Entry object. OriginalValues wrap a dictionary
// that represents the values of the entity before applying changes.
// The Entity Framework change tracker will detect
// differences between the current and original values and mark
 
Search WWH ::




Custom Search