c# - 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)
{
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!
解决方案
在您的客户实体类中,您正在调用构造函数
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
因此,当您初始化一个新对象时,您将通过构造函数中定义的两个参数firstname
和lastname
.
新的
这些由构造函数按名称设置,并且在其上下文之外(即在表存储中)没有任何意义。
CustomerEntity customer1 = new CustomerEntity("NoSolution", "Smith");
在您的代码中,您需要做的就是将您的构造函数更改为
public CustomerEntity(string requesterName, string uniqueRowKey)
{
this.PartitionKey = requesterName ;
this.RowKey = uniqueRowKey;
}
您的 RowKey 必须是唯一的,并且您的分区键用于通过对相似类型的行进行分组来简化搜索。然后,您可以像这样传递给您的构造函数:
string rowKey = Guid.NewGuid().ToString("N"); //This give you a unique guid with no hyphens.
CustomerEntity customer1 = new CustomerEntity("John Smith", rowKey);
这会将您的实体分别插入分区键和行键。
那是你要找的那种东西吗?
推荐阅读
- python - 如何提高我在迁移学习 Resnet50 中的验证准确性?
- python - 在脚本中保存对象
- wordpress - 如何在 WordPress 中阻止自定义帖子类型列表页面?
- android - 当我们在浏览器中搜索链接时,我们如何重定向到我们的 react-native 应用程序
- javascript - javascript setTimeout 函数,有问题吗
- android - 使用一个启动器分屏启动我的两个应用程序 - Android
- c# - 如何增加 ASP .NET Core 3.0 应用程序中的最大线程数
- python - python使用urllib2和urllib获取请求问题
- c++ - 我创建了一个库,想知道设计是否效率低下
- javascript - 如何在按钮点击反应中添加延迟?