The commoditization of technology has reached its pinnacle with the advent of the recent paradigm of Cloud Computing. Infosys Cloud Computing blog is a platform to exchange thoughts, ideas and opinions with Infosys experts on Cloud Computing

« 99 days to go | Main | Providing user defined status code and message in MVC action and handling it in jQuery »

Windows Azure Table storage- How to handle CAUTIOUSLY nullable data types

Azure table allows nullable type for int, string and also bool. But we need to provide special attention while updating a table entity.

For example, for a table entity - Student with properties as:

String name

Int? employeeId

Bool? IsActive

For the first time if I pass "null" for the nullable properties, it will get inserted. But if I first provide some not-null value for the concerned nullable property and then later want to update it with a null value, the old value will get retained because the table service will assume as if no data is provided. And if we really want to update with null then the default approach will not work. So to handle such scenario, in the Save operation we need to pass a change-option as:

TableServiceContext. SaveChanges(SaveChangesOptions.ReplaceOnUpdate)

This will do insert-or-replace instead of insert-or-merge and thus the concerned property will get updated with null value.

"Save changes" method without any parameter works like insert-or-merge where (for an existing entity) provided property will be updated and for others old value will be merged/retained. And thus when null is passed for any property it is assumed as if it is not provided.

So, this is an approach to work with table entity in which for any property, if null is even considered as a valid value.

Happy coding...


Thanks, precisely what I was looking for.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.