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
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Most View
- Exchange 2007: How Do I Modify a Database Size Limit?
- Windows 7 : Enhancing Your Browsing Security (part 1) - Blocking Pop-Up Windows
- Enable the Global Audit Policy by Using the Command Line
- Windows Phone 7 : Updating Your Phone Software
- SOA with .NET and Windows Azure : WCF Discovery (part 1) - Discovery Modes
- Microsoft ASP.NET 3.5 : WCF Services for ASP.NET AJAX Applications
- Securing Windows 7 : Thwarting Snoops and Crackers (part 2) - Locking Your Computer Manually, Automatically
- Windows 7 : Accessing a Shared Printer
- SQL Server 2008 : Data Encryption - SQL Server Key Management
- Windows 7 : Firing Up the Registry Editor