Inserting Records - Understanding Table Storage - Part Two

To add entries to a table, you should create objects based on the TableEntity based class and serialize them into the table using the Storage Client Library. The following properties are provided for you in this base class,

  • Partition Key: Used to partition data across storage infrastructure
  • Row Key: Unique identifier in a partition
  • Timestamp: Time of the last update maintained by Azure Storage
  • ETag: Used internally to provide optimistic concurrency
The combination of partition key and row key must be unique within the table. This combination is used for load balancing and scaling, as well as for querying and sorting entities.

Right-click your project in solution explorer - Add - New Folder

Right Click on newly created folder - Add - New Item - Select class - Name the class as Customers.cs - Click on Add

Azure 

Step 2

In the newly created class Customers.cs replaces the existing code with the following code. Make sure you are using the same name for your project, else change it accordingly.

The custom class you care reating here should be derived from the TableEntity class which lies under Microsoft.WindowsAzure.Storagetable namespace. Here we are just building the properties for this class and creating a constructor for it. We are setting the Partition-key as the type of the customer so that it stores all these records on the same storage node on the backend and the Row-key which needs to be unique within a partition as the email.

  1. using Microsoft.WindowsAzure.Storage.Table;  
  2. namespace TableDemo.Entities  
  3. {  
  4.    class Customer : TableEntity  
  5.    {  
  6.       public string CustomerName { get; set; }  
  7.       public string CustomerEmail { get; set; }  
  8.       public string CustomerType { get; set; }  
  9.       public Customer(string name, string email, string type)  
  10.       {  
  11.          this.CustomerName = name;  
  12.          this.CustomerEmail = email;  
  13.          this.CustomerType = type;  
  14.          this.PartitionKey = type;  
  15.          this.RowKey = email;  
  16.       }  
  17.    }  
  18. }  

Azure 

Step 3

Back in the Program.cs, add the following code to your Main method after the code for getting table reference. (You may delete the code to create the table if it doesn't exist as we are sure that it is already created).
  1. CreateCustomer(table, new Customer("Ramees""[email protected]""registered"));  
  2. Console.ReadKey();  

Add this method to the same class to create (insert) the customer within the table which is called in the above block of code from the main class.

  1. static void CreateCustomer(CloudTable table, Customer user)  
  2. {  
  3.    TableOperation insert = TableOperation.Insert(user);  
  4.    table.Execute(insert);  
  5.    Console.WriteLine("Record inserted");  
  6. }  

You may also need to add the following namespace to access the customer class from the entity.

  1. using TableDemo.Entities;  

Azure 

Step 4

Click on start to run the program and we can assume that the record was inserted successfully. As there was no error we got the message specified in the same block of code.

Azure