4

我的实体类

public class VerifyVariableEntity : TableEntity
{
    public VerifyVariableEntity()
    {

    }

    public VerifyVariableEntity(string consumerId, string score)
    {
        PartitionKey = consumerId;
        RowKey = score;
    }
    public string ConsumerId { get; set; }

    public string Score { get; set; }
}

我正在从 Azure 服务总线队列中获取数据,然后对其进行反序列化,最后尝试将其存储到 Azure 表存储中。下面是我从服务总线队列中获取数据并将其存储到 Azure 表存储中的实现。

class Program
{
    static void Main(string[] args)
    {
        var connectionString = "myconnectionString";

        var queueName = "myqueueName";

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        CloudTable table = tableClient.GetTableReference("test");

        table.CreateIfNotExists();

        var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
        client.OnMessage(message =>
        {
            var bodyJson = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
            var myMessage = JsonConvert.DeserializeObject<VerifyVariable>(bodyJson);
            Console.WriteLine(bodyJson);
            Console.WriteLine(myMessage.ConsumerId);
            Console.WriteLine(myMessage.Score);

            var VerifyVariableEntityObject = new VerifyVariableEntity()
            {
                ConsumerId = myMessage.ConsumerId,
                Score = myMessage.Score
            };

            TableOperation insertOperation = TableOperation.Insert(VerifyVariableEntityObject);
            // Execute the insert operation.
            table.Execute(insertOperation);
        });


        Console.ReadLine();
    }
}
4

2 回答 2

4

根据您提供的错误信息和代码,我发现您的实体构造不正确。在将实体插入 Azure 表之前,需要指定PartitionKey和属性。RowKey您可以尝试如下修改您的代码:

var VerifyVariableEntityObject = new VerifyVariableEntity()
{
   ConsumerId = myMessage.ConsumerId,
   Score = myMessage.Score,
   PartitionKey=myMessage.ConsumerId,
   RowKey=myMessage.Score
};

或者

var VerifyVariableEntityObject = new VerifyVariableEntity(myMessage.ConsumerId,myMessage.Score)
{
   ConsumerId = myMessage.ConsumerId,
   Score = myMessage.Score
};
于 2016-11-17T02:39:24.447 回答
1

在我的情况下,我在开始使用 Visual Studio 2022 后使用 Azure Storage Delete entity 命令时看到了这个错误,如下面的代码。

using Microsoft.WindowsAzure.Storage.Table.CloudTable
....
var tableOperation = TableOperation.Delete(entity);
return await CloudTable.ExecuteAsync(tableOperation);

VS 2022 使用Azurite而不是旧版Azure 存储模拟器,这似乎是模拟器的问题,因为该代码适用于 VS 2019 和实时 Azure 环境。

因此,如果您可以开始使用 Visual Studio 2019,直到 Azurite 变得更加稳定。

于 2021-11-30T11:32:14.027 回答