Database Reference
In-Depth Information
To delete a comment, we call the Entity() method on the context object, passing in the comment entity as an
argument and set the EntityState to Deleted , which marks the comment for deletion and generates a SQL Delete
statement.
Finally, the GetPostByTitle() method eagerly loads the comments for each post and returns an object graph of
posts and related comments. Because we have implemented POCO classes, Entity Framework returns what is called
a dynamic proxy object that wraps the underlying post and comments class. Unfortunately, WCF cannot serialize a
proxy object. However, with the line ProxyCreationEnabled = false , we simply disable proxy class generation for
the query and Entity Framework returns the actual objects. If we attempted to serialize the proxy object, we would
receive the following error message:
The underlying connection was closed: The connection was closed unexpectedly
We could even move the ProxyCreationEnabled = false to the constructor of the service to enforce it for all the
service methods.
In this recipe, we've seen that we can use POCO objects to handle CRUD operations with WCF. Because there
is no state information stored on the client, we've built separate methods for inserting, updating, and deleting posts
and comments. Other recipes in this chapter will demonstrate techniques used to reduce the number of methods our
service must implement and to simplify the communication between the client and the server.
9-3. Finding Out What Has Changed with Web API
Problem
You want to leverage REST-based Web API services for database insert, delete, and update operations for an object
graph without having to expose a separate method for updating each entity class. Additionally, you want to implement
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, to allow for easier configuring, debugging and simulation of an n-Tier application.
Let's say that you have a model like the one shown in Figure 9-3 .
Figure 9-3. A model for Travel Agents and Bookings
 
 
Search WWH ::




Custom Search