【问题标题】:How to auto generate a RowKey in a Azure table storage using C#如何使用 C# 在 Azure 表存储中自动生成 RowKey
【发布时间】:2018-08-08 18:25:38
【问题描述】:

我正在尝试将 RowKey 从 Micorosft Docs 教程中预定义的姓氏:https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-dotnet#add-an-entity-to-a-table 更改为唯一值。

这是我当前的代码:

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; }
}

任何有关此问题的见解帮助将不胜感激!

【问题讨论】:

  • 不知道你在问什么。您可以使用任何您想要的分区键和行键,只要两者的组合 (pk+rk) 是唯一的。请编辑您的问题以更具体。另外,请注意,每次调用 CreateTableIfNotExists() 都需要调用表存储,如果您在设置/启动过程中创建表,则无需调用。

标签: c# azure azure-table-storage row-key


【解决方案1】:

在您的客户实体类中,您调用的是 Constructor

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

因此,当您初始化一个新对象时,您会传递两个参数,正如构造函数中定义的那样,firstnamelastname

新的

这些由构造函数按名称设置,并且在其上下文之外(即在表存储中)没有任何意义。

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);

这会将您的实体分别插入分区键和行键。

这就是你要找的东西吗?

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 2017-03-28
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    相关资源
    最近更新 更多