How to auto generate a RowKey in a Azure table storage using C#

5.1k views Asked by At

I am trying to change the RowKey from a predefined lastname from the Micorosft Docs tutorial: https://learn.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-dotnet#add-an-entity-to-a-table, to a unique value.

This is my current code:

private void storeuserinput(Activity activity)
        {
            var uid = activity.From.Id;
            var uname = activity.From.Name;

            if (activity.Text?.ToLower().ToString() == "no" || activity.Text?.ToLower().ToString() == "NO" || activity.Text?.ToLower().ToString() == "No" || activity.Text?.ToLower().ToString() == "Nope" || activity.Text?.ToLower().ToString() == "nope")
            {
                var userinput = firstmessage;

                string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");

                // Parse the connection string and return a reference to the storage account.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                // Retrieve a reference to the table.
                CloudTable table = tableClient.GetTableReference("UnansweredChatBot");

                // Create the table if it doesn't exist.
                table.CreateIfNotExists();

                // Create a new customer entity.
                CustomerEntity customer1 = new CustomerEntity("NoSolution", "Smith");
                customer1.Query = firstmessage;

                // Create the TableOperation object that inserts the customer entity.
                TableOperation insertOperation = TableOperation.Insert(customer1);

                // Execute the insert operation.
                table.Execute(insertOperation);
            }

            //extract other data from "activity" object

            //your code logic here
            //store data in your table storage

            //Note: specifcial scenario of user send attachment
        }

public class CustomerEntity : TableEntity
{
    public CustomerEntity(string lastName, string firstName)
    {
        this.PartitionKey = lastName;
        this.RowKey = firstName;
    }

public CustomerEntity() { } // the parameter-less constructor must be provided

public string Query { get; set; }
}

Any insight help with this problem would be much appreciated!

1

There are 1 answers

0
Pete Philters On BEST ANSWER

In your customer entity class you are calling the Constructor

public CustomerEntity(string lastName, string firstName)
{ 
    this.PartitionKey = lastName;
    this.RowKey = firstName;
}

So when you initialise a new object you pass through two parameters, as defined in the constructor, firstname and lastname.

new

These are set by name by the constructor and hold no meaning outside their context (i.e. in the table store).

CustomerEntity customer1 = new CustomerEntity("NoSolution", "Smith");

In your code all you need to do is change your constructor to be like

public CustomerEntity(string requesterName, string uniqueRowKey)
{ 
    this.PartitionKey = requesterName ;
    this.RowKey = uniqueRowKey;
}

Your RowKey must be unique and your partition key is used to make searching easier by grouping rows of similar types. You can then pass through to your constructor like this:

string rowKey = Guid.NewGuid().ToString("N"); //This give you a unique guid with no hyphens.
CustomerEntity customer1 = new CustomerEntity("John Smith", rowKey);

Which would insert your entity with that set to the Partition Key and Row Key respectively.

Is that the sort of thing you were looking for?