Database Reference
In-Depth Information
Subsequently in the client, we add another new travel agent and booking, using the PostAsync method to call the
Update action method in the service, using the PostAsync method which again inserts each of the new objects into
the database.
Next, we modify the name of the second agent and move one the bookings from the first agent to the second.
This time, when we call the Update method, each of the entities has an Id property with a value greater than 1 , and
thus we set the entity state as modified, causing SQL Updates to be issued by the Entity Framework.
Finally, the client calls the Retrieve Action Method on the service leveraging the GetAsync method exposed
by the HttpClient API. The Retrieve method is invoked, and returns all of the travel agent and booking entities.
Here we simply implement eager loading with the Include() method, which returns all of the properties in each
child booking entity.
Be aware that the JSON serializer will return all public properties in an entity object, even if you only project
(for example, select) a subset of the properties.
In this recipe, we've seen that we can encapsulate Entity Framework data operations behind a Web API service.
The client can consume the service by using the HttpClient object that is exposed by the Web API client API. In this
example, we moved away from the Web API's preferred HTTP verb-based dispatch and implemented more of an
RPC-based routing approach . In production applications, you'd most likely want to utilize the HTTP verb-based
approach, as it fits into the underlying intent of the ASP.NET Web API, which is to expose REST-based services.
In a production application, we would most likely create another layer (Visual Studio class project) to separate
the Entity Framework data access code from the Web API service.
9-4. Implementing Client-Side Change Tracking with Web API
Problem
You want to leverage REST-based Web API services for database insert, delete, and update operations to an object
graph while implementing a reusable client-side approach to updating entity classes. Additionally, you want to
leverage the code-first approach for Entity Framework 6 to manage data access.
In this example, we emulate an n-Tier scenario where a stand-alone client application (Console Application) is
calling a stand-alone website (Web API Project) that exposes REST-based services.
Note that each tier is contained in a separate Visual Studio Solution, so as to allow for easier configuring,
debugging, and simulation of an n-Tier application.
Solution
Let's say that you have a model like the one shown in Figure 9-4 .
Figure 9-4. A Customer and Phone Numbers model
 
Search WWH ::




Custom Search