Programming4us
         
 
 
Programming

Programming Windows Azure : Table Operations - Updating Entities

11/21/2010 11:37:36 AM

It wouldn’t be much fun if you couldn’t update data once you put it in storage, would it? Thankfully, updating entities in Azure tables is pretty simple once you get the hang of the various merge/conflict options available.

Earlier, you saw how every query result returns with an ETag. This ETag corresponds to a specific version of the entity. If the entity changes, the service will ensure that it gets a new ETag. Updating an entity now becomes a simple matter of retrieving entities, making changes on the client side, and telling the server about your updates and what the ETag was when you first retrieved the entity to detect conflicts. This is similar to concurrency controls in databases.

In .NET code, this is tightly integrated with the DataServiceContext. You have several “merge” options from which you can choose, as shown in Table 1.

Table 1. Merge options
Merge optionDescription
AppendOnlyClient changes for new entities are accepted, but changes to existing entities are not. This is the default value where the entity isn’t loaded from the server if it is present in the client cache.
OverwriteChangesAlways update with values from the server, overwriting any client changes.
PreserveChangesClient-side values that have changed are preserved, but other values are updated from the server. Also, when an entity instance exists on the client, it is not loaded from the server. No client-side changes are lost. When an object is updated, the ETag is updated as well, so this catches any errors if changes have occurred on the server without the client’s knowledge.
NoTrackingThere is no client-side tracking. Values are always read from the storage source, and any local changes are overwritten.

In code, to update an entity, you typically follow these steps:

  1. Create a DataServiceContext object, and set its merge option to one of the selections from Table 10-7. You could choose to leave the default value (AppendOnly), because this works well in normal scenarios.

  2. Query for the entity you want to update.

  3. Update the object representation of your entity in code.

  4. Call DataServiceContext.UpdateObject on the objects you updated.

  5. Call DataServiceContext.SaveChanges to push changes back to the server.

Example 1 walks through a simple query-update-save pattern.

Example 1. Simple update
CloudStorageAccount.Parse(ConfigurationSettings.AppSettings
["DataConnectionString"]);
var svc = new TestDataServiceContext
(account.TableEndpoint.ToString(), account.Credentials);
svc.MergeOption = MergeOption.PreserveChanges;
var query = from contact in svc.CreateQuery<Contact>("ContactTable")
where contact.Name == "Steve Jobs"
select contact;
var foundContact = query.FirstOrDefault<Contact>();
foundContact.Address = "One Infinite Loop, Cupertino, CA 95014";
svc.UpdateObject(foundContact);

svc.SaveChanges();


The following HTTP traffic when SaveChanges is called shows this ETag matching mechanism clearly. You already saw how querying for an item sends down an ETag, which is the server’s version of the object. When SaveChanges is called, you send out an update that says, “Update if it matches the earlier ETag.” If successful, the server updates the entity and sends down a new ETag.

HTTP Request (only headers shown)

MERGE /ContactTable(PartitionKey='a844fa27-7ae2-4894-9cc6-dd0dbdcd5ec4',
RowKey='') HTTP/1.1
User-Agent: Microsoft ADO.NET Data Services
x-ms-date: Tue, 21 Apr 2009 16:57:29 GMT
Authorization: SharedKeyLite sriramk:HVNqyS/X/AhqFxhnRKSe0SXTDSzJssFyk9JiyQvwnO4=
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 1.0;NetFx
Content-Type: application/atom+xml
If-Match: W/"datetime'2009-04-21T06%3A38%3A28.242Z'"
Host: sriramk.table.core.windows.net
Content-Length: 864

HTTP Response
HTTP/1.1 204 No Content
Cache-Control: no-cache
Content-Length: 0
ETag: W/"datetime'2009-04-21T16%3A56%3A29.717Z'"
Server: Table Service Version 1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 05a9f45f-bab9-417a-89b6-fc7759f31b2f
Date: Tue, 21 Apr 2009 16:56:35 GMT

Other -----------------
- Programming Windows Azure : Table Operations - Understanding Pagination
- Programming Windows Azure : Table Operations - Using Partitioning
- Programming Windows Azure : Table Operations - Querying Data
- Programming Windows Azure : Table Operations - Creating Entities
- Programming Windows Azure : Table Operations - Creating Tables
- iPad Development : Document Management (part 2)
- iPad Development : Document Management (part 1)
- iPad Development : The Split View Concept
- jQuery 1.3 : Developing plugins - Adding new shortcut methods
- jQuery 1.3 : Developing plugins - DOM traversal methods
- Using Cloud Services : Exploring Online Planning and Task Management
- Using Cloud Services : Exploring Online Scheduling Applications
- Using Cloud Services : Exploring Online Calendar Applications
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 3)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 2)
- SOA with .NET and Windows Azure : Service Contracts with WCF (part 1)
- Cloud Security and Privacy : Data Security and Storage
- iPad SDK : Working with Documents - Desktop Synchronization
- Required Project Images for iPad Apps
- iPhone SDK : GameKit Voice Chat
 
 
Most View
- Programming WCF Services : Data Contracts - Collections (part 2) - The CollectionDataContract Attribute & Dictionaries
- Windows 7 : Enabling MAC Address Filtering
- SharePoint 2010 : Get Started with Your Personal Site
- Windows Server 2008 : Controlling Access to Web Services (part 1) - Understanding Anonymous Authentication
- SQL Server 2008 : Implementing Error Handling - Understanding Errors
- Windows 7 : Creating a Windows Network - Choosing a Network and Cabling System
- Programming Windows Azure : Table Operations - Using Partitioning
- Windows Phone 7 : Working with Maps
- Windows 7 : Enabling the Shutdown Event Tracker
- Auditing an Existing Site to Identify SEO Problems (part 1 - Elements of an Audit
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS