Database Reference
In-Depth Information
5.
To test our service, we'll need a client. Add a new Windows Console Application project to
the solution. Use the code in Listing 9-32 for the client. Add a service reference to the client
by right-clicking the client project and selecting Add Service Reference. You may need to
right-click the service project and select Debug Start Instance to start an instance of
your service before you can add a service reference in the client.
Listing 9-32. The Client We Use to Test Our WCF Service
class Program
{
static void Main(string[] args)
{
var service = new Service1Client();
var order = service.InsertOrder();
order.Quantity = 5;
service.UpdateOrderWithoutRetrieving(order);
order = service.InsertOrder();
order.Quantity = 3;
service.UpdateOrderByRetrieving(order);
}
}
If you set a breakpoint on the first line in the Main() method of the client and debug the application, you can step
through inserting the order and updating the order using both methods.
How It Works
Our InsertOrder() method (see Listing 9-31) deletes any previous test data, inserts a new order, and returns the
order. The order returned has both the database generated OrderId and TimeStamp properties. In our client, we use
two slightly different approaches to update this order.
In the first approach, UpdateOrderWithoutRetrieving() , we Attach() the order from the client and check
whether the order status is Received , and, if it is, we mark the entity's Quantity property as modified and call
SaveChanges() . Entity Framework will generate an update statement setting the new quantity with a where clause that
includes both the OrderId and the TimeStamp values from the Order entity. If the TimeStamp value has changed by
some intermediate update to the database, this update will fail. To capture and handle such a concurrency exception,
we wrap the operation with Try/Catch construct, trap for an OptimisticConcurrencyException, and handle the
exception. This ensures that the Order entity we are updating has not been modified between the time we obtained
it from the InsertOrder() method and the time we updated it in the database. Note in the example how all entity
properties are updated, whether they have changed or not.
Alternately, you could explicitly check the concurrency of an entity before performing an update. Here you
could retrieve the target entity from the database and manually compare the TimeStamp properties to determine
whether an intervening change has occurred. This approach is illustrated in Listing 9-31 with a fresh order by calling
the UpdateOrderByRetrieving() method. Although not foolproof (the order could be changed by another client
between the time you retrieve the order from the database, compare TimeStamp values, and call SaveChanges() ), this
approach does provide valuable insight into what properties or associations have changed on an entity. Although not
efficient as the first approach, this method might be useful if the object graph or entities are large or complex.
 
Search WWH ::




Custom Search