Search code examples
azure-table-storageazure-servicebus-queuesazure-tablequery

Getting "The values are not specified for all properties in the entity" error while using Azure Table Storage


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

Solution

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