Table Storage is a key-value store that uses a partition key to help with scale out distribution of data and a row key for unique access to a particular entry within a partition. Together, these keys are used to uniquely identify a record in the account.
Accessing the account
First, we will need to add the following to our configuration file (app.config if it’s a console application, web.config if it’s a web application):
Then, we will access our account with the following code:
Creating a Table
To create a Table, we can use the
CreateIfNotExists function, as shown in the sample below:
To insert a record we will first need a class that inherits from
Then, we create a new
OrderEntity, fill the values, and execute an Insert operation on the Table:
Inserting multiple records
We can also insert multiple records in a batch transaction, with the following limitations:
- All operations must take place on the same partition.
- The batch can include up to 100 operations.
- The batch payload cannot be greater than 4 MB.
To update a record, we first get the record, then we can create an
Deleting a record
To delete a record, we must first retrieve the record (as shown in the previous code sample) and the create a
Querying Azure Table Storage
In the first versions of the Azure Storage library (versions 1.x), the experience was much like that of an Entity Repository, in the sense that we had a Context and lots of LINQ calls. Although it was easy to understand, it was really difficult to determine exactly when the query was done to the service. And it was pretty upsetting to be limited only to “Where”, “First” and “Select” LINQ functions.
Since version 2.0, we have a
TableQuery object available, which gives us more control over our query, and we can better understand what’s happening.
Querying for an entity
To query for an entity, we will need the partition key and the row key.
Table Storage supports OData, which allows us to make some basic querying: filter, top, select, orderby and a few others. The Storage Client library exposes then through the
This allows us, for example, to get all records in a partition:
There are two important elements here:
There are some variations to the
GenerateFilterCondition function, according to the data type, being this one the one used for strings.
And the available options for
If we need to combine different filters, we use the
With the operators being:
Select function allows us to query only for the fields we need. This function can be really useful in the cases when we need to save network bandwidth, like when we are building a WinRT app.
For example, if we want to get only the names of our customers:
If we need to limit the amount of results, we just use the
Of course, we can combine all these functions together:
Strongly typed queries
All the previous example functions return dynamic values, but we can also make queries that return the class we need, by using