1

For example, I have a bool? Status in c#

when the Status = true/false, it can save to Azure table, no problem

But when Stats = null, it cannot save(update) to Azure table, the column still keeps the old value

I guess it might because Azure table does not have a scheme, but whats the solution?

How to save a null to overwrite the original value?

EDIT, the code

data like this:

public class someEntity : TableServiceEntity
{
    public bool? Status { get; set; }
}

update like this:

tableContext.AttachTo("sometable", someEntity);
tableContext.UpdateObject(someEntity);
tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch & SaveChangesOptions.ReplaceOnUpdate);

(I tried AttachTo with "*" as etag, tried remove SaveChangesOptions, neither work)

SORRY for my stupid, should be this code, then works

tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
  • 1
    Is the field in the DB allowed to be null? – f2lollpll Jan 16 '12 at 07:09
  • I believe yes, if initial value is null, then you can still save. But once you saved as true/false, you can never set back to null again. – Eric Yin Jan 16 '12 at 07:13
  • 1
    Can you post your code? It's most likely an issue with Nullable default value being null, and the API disregarding the default value without an explicit update/upsert flag. – lukiffer Jan 16 '12 at 07:31
  • @EricYin Since you've solved this for yourself, I suggest you remove your last update and post it as an answer instead. – Matthew Strawbridge Jan 16 '12 at 22:08

1 Answers1

4

As @Matthew suggested, I put the answer here

very simple, use:

tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);

For multiple options, use | (or), not & (and). Reason

SaveChangesOptions.ReplaceOnUpdate = true means the whole entity will be replaced SaveChangesOptions.ReplaceOnUpdate = false, means merge, the old data will be kept

Eric Yin
  • 8,737
  • 19
  • 77
  • 118