سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top