Creating Entities in Azure Table Storage

This document guides you through the process of creating entities within your Azure Table Storage tables. Entities are the fundamental units of data stored in Azure Tables. Each entity is a set of properties, represented as key-value pairs.

Prerequisites

Before you begin, ensure you have:

Understanding Entity Properties

Every entity in Azure Table Storage must include two system properties:

In addition to these system properties, you can define custom properties. Property names are case-insensitive strings and can contain up to 255 characters. Property values can be of various Azure Table Storage data types.

Methods for Creating Entities

You can create entities using various methods, including the Azure SDKs, Azure CLI, Azure PowerShell, or REST API.

Using Azure SDKs (Example: .NET)

The Azure SDKs provide convenient ways to interact with Table Storage. Here's a C# example using the Azure SDK for .NET:


using Azure;
using Azure.Data.Tables;

// Replace with your connection string and table name
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "Tasks";

// Authenticate to Azure Table Storage
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
TableClient tableClient = tableServiceClient.GetTableClient(tableName);

// Create a new entity
var task = new
{
    PartitionKey = "TasksToDo",
    RowKey = Guid.NewGuid().ToString(), // Unique RowKey for each entity
    Description = "Buy groceries",
    IsCompleted = false,
    DueDate = DateTimeOffset.UtcNow.AddDays(1)
};

try
{
    tableClient.UpsertEntity(BinaryData.FromObjectAsJson(task), TableUpdateMode.Replace);
    Console.WriteLine("Entity created successfully.");
}
catch (RequestFailedException e)
{
    Console.WriteLine($"Error creating entity: {e.Message}");
}
            

Using Azure CLI

You can also create entities using the Azure Command-Line Interface (CLI).

Create an Entity using `az storage table entity insert`

To insert an entity, you'll typically use a JSON file containing the entity's properties.

Example JSON file (entity.json)

{
    "PartitionKey": "TasksToDo",
    "RowKey": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
    "Description": "Schedule dentist appointment",
    "IsCompleted": false,
    "DueDate": "2023-12-31T10:00:00Z"
}
            
Azure CLI command

az storage table entity insert \
    --connection-string "YOUR_AZURE_STORAGE_CONNECTION_STRING" \
    --table-name Tasks \
    --entity @entity.json
            

Important Considerations

When creating entities, keep the following in mind:

Tip: For unique entities within a partition, consider using GUIDs for your RowKey. For sequential or related entities, you might use timestamps or other identifiers.

Next Steps

Once you have created entities, you will likely want to query them. Refer to the Querying Entities document for more information.