Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

Modifying entities with the REST API is CRUD (part 1) - Inserting entities

3/16/2011 10:08:21 PM
- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

12.3.1. Inserting entities

Before we look at how to insert an entity using the REST API, let’s look at the code you’d write to store an entity in a table using the StorageClient library:

var shirtContext = new ProductContext();

shirtContext.AddObject("Products",
new Product
{
PartitionKey = "Shirts",
RowKey = "RedShirt",
Name = "Red Shirt",
Description = "A Red Shirt"
});

shirtContext.SaveChanges();

The preceding example inserts a new instance of the Product entity into the Products table. The new Product will be stored in the Shirts partition with a RowKey value of RedShirt

The Unit of Work pattern

WCF Data Services, and therefore the StorageClient library, implement the Unit of Work pattern for saving data back to the database. This means that all changes are tracked locally (when you use the AddObject method) and then all changes are saved back to the Table service when the SaveChanges method is called.

A process that doesn’t use the Unit of Work pattern would not track the changes to the entities locally and apply the changes directly to the Table service when the AddObject method is called. This removes any batching or cancellation capabilities easily provided by the Unit of Work pattern.


Now that we’ve reminded ourselves of how to insert a new entity into a table using the StorageClient library, let’s look at how this would be done using the REST API directly.

Using the REST API

To use the AtomPub-based REST API to insert a new entity into a table, you’d need to perform an HTTP POST request to the following URI:

http://<storageaccountname>.table.core.windows.net/<TableName>

To insert a new entity into the Products table in the silverlightukstorage storage account, you’d use the following URI:

http://silverlightukstorage.table.core.windows.net/Products

To insert the entity we created at the beginning of section 1 with the StorageClient library, you’d need to define the request body with the following Atom XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<updated>2009-07-27T14:22:48.8875037Z</updated>
<author>
<name />
</author>
<id />
<content type="application/xml">
<m:properties>
<d:Description>A Red Shirt</d:Description>
<d:Name>Red Shirt</d:Name>
<d:PartitionKey>Shirts</d:PartitionKey>
<d:RowKey>RedShirt</d:RowKey>
<d:Timestamp m:type="Edm.DateTime">0001-01-01T00:00:00</d:Timestamp>
</m:properties>
</content>
</entry>


If you look at the preceding AtomPub XML, you can see that it follows a similar format to the XML used to create the storage account table in section 1 of this article.. As you can see from the preceding Atom XML, not only are the values of each property of the entity included in the XML, but so is the name and type of each property (notice that the Timestamp property is of type Edm.DateTime). Because a Table service table is effectively schemaless, and each row could contain an entity with an entirely different set of properties, it’s important that the entities being inserted into the table be self-describing.

If you wanted to modify the console application in listing 2 to insert an entity instead of creating a table, you could replace the URI generation with the URI we defined earlier and replace the Atom XML with the preceding document.

Now that you’ve created your lovely entity, let’s nuke it!

Other -----------------
- Working with the Table service REST API - Authenticating requests against the Table service
- Content delivery networks
- Using BLOB storage as a media server (part 3) - A Silverlight-based chunking media player
- Using BLOB storage as a media server (part 2) - A WPF-based adaptive-streaming video player
- Using BLOB storage as a media server (part 1) - Building a Silverlight or WPF video player
- Hosting Silverlight applications in BLOB storage (part 2) - Communicating with third-party sites
- Hosting Silverlight applications in BLOB storage (part 1) - Hosting the Silverlight Spectrum emulator
- Hosting static HTML websites (part 2) - Publishing your website to BLOB services
- Hosting static HTML websites (part 1) - Creating a static HTML website
- Performing storage account operations using REST (part 3) - Creating a table using the REST API
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
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 windows Phone 7 windows Phone 8
programming4us programming4us
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server