Database Reference
In-Depth Information
// update a comment
comment1.CommentText = "How do I use ICollection<T>?";
client.SubmitComment(comment1);
// delete comment 1
client.DeleteComment(comment1);
// get posts with comments
var p = client.GetPostByTitle("Change Tracking Proxies");
Console.WriteLine("Comments for post: {0}", p.Title);
foreach (var comment in p.Comments)
{
Console.WriteLine("\tComment: {0}", comment.CommentText);
}
}
}
}
The following is the output of our test client from Listing 9-11:
Comments for post: Change Tracking Proxies
Comment: I use ICollection<T> all the time
How It Works
Let's start with the Windows console application, which is our test client for the service. We create an instance of our
service client in a using {} block. Just as we've done when creating an instance of an Entity Framework context in a using
{} block, this ensures that Dispose() is implicitly called when we leave the block, either normally or via an exception.
Once we have an instance of our service client, the first thing we do is call the Cleanup() method. We do this to
remove any previous test data we might have.
With the next couple of lines, we call the service's SubmitPost() method. In this method's implementation
(see Listing 9-10), we interrogate the value of PostId. If the PostId is 0, we then assume it's a new post and set the
Entity State to Added . Otherwise, we assume the entity exists and we are modifying it, thus setting the Entity State
to Modified . Although somewhat crude, this approach can determine the state (new or existing) of the post entity,
depending on the domain of the valid Ids for a post as well as the runtime initializing integers to 0. A better approach
might involve sending an additional parameter to the method or creating a separate InsertPost() method. The best
approach depends on the structure of your application.
If the post is to be inserted, we change the object state of the post to EntityState.Added . Otherwise, we change its
object state to EntityState.Modified . The EntityState value drives whether an insert or update statement is generated.
If the post is inserted, the post instance's PostId is updated with the new correct value. The post is returned.
Inserting and updating a single property on the Comment entity is similar to inserting and updating a post with
one significant difference: as a business rule, when we update a comment, we want to make sure only to update the
CommentText property. This property holds the body of the comment, and we don't want to update any other part
of the Comment entity object. To do this, we mark just the CommentText property as modified. Entity Framework
will generate a simple update statement that changes just the CommentText column in the database. Note that this
works as we are just changing a single property of the entity. If we were changing multiple properties on the Comment
entity, we would then need some way to track which properties were changed on the client. In cases where multiple
properties can change, it is often more efficient to update the entity object, without need for complex client-side
change tracking.
 
 
Search WWH ::




Custom Search