2

I have user table which has an attribute called "PictureUri" of string type. By default its null and then i update it to some text. Later if i try to update it to null again, it doesnt happen. The update is successful but the string attribute doesn't change to null. I can update the same attribute to some other text but just not to null.

Does azure table storage doesn't allow me to update attributes to null?

Here's a sample code:

Model:

public class User
    {
        public string PictureUri{ get; set; }
        public string Email { get; set; }
        public string Username { get; set; }
    }

[System.Data.Services.Common.DataServiceKey(new string[] { "PartitionKey", "RowKey" })]

    public class PersistedUser : User,ITableEntity
    {
        public string PartitionKey
        {
            get { return this.Email; }
            set { this.Email = value; }
        }

        public string RowKey
        {
            get { return this.Username; }
            set { this.Username = value; }
        }

        public DateTime Timestamp { get; set; }

    }

Update API

user.PictureUri = null;
  tableServiceContext.AttachTo(TableNames.User, user);
        tableServiceContext.Update(user);
        tableServiceContext.SaveChanges(System.Data.Services.Client.SaveChangesOptions.ReplaceOnUpdate);

        var tempUser = this.tableServiceContext.QueryableEntities.Where(u => u.RowKey.Equals(user.Username, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

        tableServiceContext.Detach(user);

EDIT:

Using SaveChangesOptions.ReplaceOnUpdate updates the value in table but when i try to query for it, it returns the old data. Any idea what the problem might be?

1 Answer 1

5

Try using the following SaveChanges() and provide ReplaceOnUpdate as the SaveChangesOption. Something like:

        user.PictureUri = null;
        tableServiceContext.AttachTo(TableNames.User, user);
        tableServiceContext.Update(user);
        tableServiceContext.SaveChanges(SaveChangesOption.ReplaceOnUpdate);
        tableServiceContext.Detach(user);

I think what's happening is that the default save option is "Merge" where it does not change the values which are not passed (i.e. passed as null).

Other option could be to set user.Picture1 to String.Empty instead of null.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response. But with your first approach, the table gets updated properly but when i try to query it the immediately after that i get the old data. Any idea what the reason could be?
This should not be happening as Azure Table Storage is strongly consistent. Can you share complete code?
Just a guess, but can you try executing your query after you detach the user i.e. after the line tableServiceContext.Detach(user).
naah doesnt change the result. If I update it to something other than null then am getting the record correctly immediately. Only when am updating it to null, the query returns the old one. Any idea?
Could it be because of attaching and detaching? I still dont know why i have to do that. Why isnt the resource by default attached to the context? Is this is the best way to update an entity in table storage?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.