Guy Ben-Moshe
Guy Ben-Moshe

Reputation: 934

Azure Tables - How to update a value

It's really basic but I couldnt find sources that helps me with that, this is my entity:

public class PetEntity : TableEntity
        {
            public PetEntity(String petName)
            {
                this.PartitionKey = petName;
            }
            public PetEntity() { }
            public int lvl { get; set; }
            public int exp { get; set; }
            public int hp { get; set; }
            public int ap { get; set; }
            public int dp { get; set; }
        }

I made a loop to increase the current hp of all the pets by 1 and it looks like that:

foreach (PetEntity entity in table.ExecuteQuery(query))
                {
                    entity.hp++;
                    Trace.TraceInformation("HP of {0} enhanced to {1}.", entity.PartitionKey, entity.hp);
                }

The problem is the enhanced HP does update for the first output but next time I reach entity.hp it stays the same, the value won't update.

Note - it reaches the entities, that's not the problem.

Upvotes: 1

Views: 1549

Answers (1)

Matias Quaranta
Matias Quaranta

Reputation: 15603

You have to manually replace the entity once you alter one of it's values:

foreach (PetEntity entity in table.ExecuteQuery(query))
{
    entity.hp++;
    table.Execute(TableOperation.Replace(entity));
    Trace.TraceInformation("HP of {0} enhanced to {1}.", entity.PartitionKey, entity.hp);
}

Upvotes: 4

Related Questions