Database Reference
In-Depth Information
foreach (var phoneType in customer.Phones)
{
Console.WriteLine("Phone Type: {0}", phoneType.PhoneType);
}
}
}
else
{
Console.WriteLine("{0} ({1})", (int) _response.StatusCode, _response.ReasonPhrase);
}
}
}
12.
Finally, add the same Customer, Phone, BaseEntity, and TrackingState classes that we
added to service in Listings 9-19 and 9-20.
Following is the output of our test client from Listing 9-26:
Successfully created Customer Geroge Bush and 2 Phone Numbers(s)
Added Phone Type: White House Red Phone
Added Phone Type: Bush Mobile Phone
Successfully created Customer Barrack Obama and 2 Phone Numbers(s)
Added Phone Type: Obama Mobile Phone
Added Phone Type: White House Red Phone
Removed Geroge Bush from database
Remove Bush Mobile Phone from data store
Customer Barrack Obama has 2 Phone Numbers(s)
Phone Type: White House Red Phone
Phone Type: Obama Mobile Phone
How It Works
Start by running the Web API application. The Web API application contains an MVC Web Controller that, when
started, will bring up a home page. At this point, the site is running and its services are available.
Next open the console application, set a breakpoint on the first line of code in the program.cs file, and run the
console application. We first establish some basic plumbing—mapping the Web API service URI and configuring the
Accept Header—that will ask the Web API service to return the data in a JSON format.
We then call the Cleanup action method on the Customer Web API controller using the Client.DeleteAsync
method exposed by the HttpClient object. This call invokes the Cleanup action method in the service and truncates
data from the database tables for any previous operations.
Back in the client, we create a new customer and two phone number objects. Note how we explicitly set the
TrackingState property for each entity in the client to instruct the Entity Framework Change Tracking Components of
the SQL operation required for each entity.
We then invoke the UpdateCustomer action method from the service by making a call to the PostAsync method
from the HttpClient object. If you place a breakpoint in the UpdateCustomer Action Method in the Customer Web API
controller class, you'll see that it receives the Customer object as a parameter and immediately adds it to the context
object. Doing so marks the object graph as added, and it causes the context to start tracking it.
Interestingly, we next hook into the underlying DbChangeTracker, which is exposed as a property of the context
object. DbChangeTracker exposes a generic IEnumerable type of <DbEntityEntry> entitled Entries . We simply
assign the base EntityType to it. Doing so allows us to enumerate through each of the entities in the context that are of
 
Search WWH ::




Custom Search