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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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



I am trying to change the RowKey from a predefined lastname from the Micorosft Docs tutorial: https://docs.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)


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!





Not sure exactly what you're asking. You can use anything you want for partition key and row key, as long as the combination of the two (pk+rk) is unique. Please edit your question to be more specific. Also, note that each call to CreateTableIfNotExists() requires a call to Table Storage, which is unnecessary if you create your table as part of your setup/startup.
– David Makogon
Aug 8 at 18:58


CreateTableIfNotExists()




1 Answer
1



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.


firstname


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?






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard