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.
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!