4

My Entity Class

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

I am fetching the data from Azure Service Bus queue, then deserialize it and finally trying to store it into Azure Table Storage. Below is my implementation for fetching the data from Service Bus Queue and storing it into Azure Table Storage.

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();
    }
}
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83

2 Answers2

4

According to the error message and code you provided, I found that your entity was not constructed correctly. The PartitionKey and RowKey properties need to be specified before you inserting the entity to Azure Table. You could try to modify your code as follows:

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

or

var VerifyVariableEntityObject = new VerifyVariableEntity(myMessage.ConsumerId,myMessage.Score)
{
   ConsumerId = myMessage.ConsumerId,
   Score = myMessage.Score
};
Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • I can retrieve the inserted record through code, but I can't see the records on Azure Portal under the table in Cloud Storage. I am new to Azure, so I was wondering how much time does it take to reflect an inserted record on Azure Portal. – tRuEsAtM Nov 17 '16 at 17:54
  • 1
    You could only see table name list on Azure Portal. Here is the official tool [Azure Storage Explorer](http://storageexplorer.com/) for you to mange your storage resources. And you could follow this [tutorial](https://learn.microsoft.com/en-us/azure/vs-azure-tools-storage-manage-with-storage-explorer) to get started with Azure Storage Explorer. – Bruce Chen Nov 18 '16 at 01:40
1

In my case I saw this error when using Azure Storage Delete entity command after I started using Visual Studio 2022 like following code.

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

VS 2022 use Azurite instead of legacy Azure Storage Emulator and it seems to be a issue with the emulator because that code is working with VS 2019 and live Azure environment.

So if you can start using Visual Studio 2019 until Azurite gets more stable.

Iman
  • 17,932
  • 6
  • 80
  • 90