Here's my problem. A class which defines an order has a property called PaymentStatus
, which is an enum
defined like so:
public enum PaymentStatuses : int
{
OnDelivery = 1,
Paid = 2,
Processed = 3,
Cleared = 4
}
And later on, in the class itself, the property definition is very simple:
public PaymentStatuses? PaymentStatus { get; set; }
However, if I try to save an order to the Azure Table Storage, I get the following exception:
System.InvalidOperationException: The type Order+PaymentStatuses' has no settable properties.
At this point I thought using enum
isn't possible, but a quick Google search returned this: http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/7eb1a2ca-6c1b-4440-b40e-012db98ccb0a
This page lists two answers, one of which seems to ignore the problems and suggests that using an enum
in Azure Storage is fine.
Now, I don't NEED to store the enum
in the Azure Table Storage as such, I could just as well store a corresponding int
, however, I do need this property to be exposed in the WCF service.
I've tried making the property use get
and set
to return the enum
from a stored integer
, and remove this property from Azure by using the WritingEntity
event on my DataContext
, but I get that exception before the event for this entity is fired.
At this point, I'm at a loss, I don't know what else I can do to have this property in WCF as an enum
, but have Azure store just the int
.
I have come across a similar problem and have implemented a generic object flattener/recomposer API that will flatten your complex entities into flat
EntityProperty
dictionaries and make them writeable to Table Storage, in the form ofDynamicTableEntity
.Same API will then recompose the entire complex object back from the
EntityProperty
dictionary of theDynamicTableEntity
.This is relevant to your question because the ObjectFlattenerRecomposer API supports flattening property types that are normally not writeable to Azure Table Storage like
Enum
,TimeSpan
, allNullable
types,ulong
anduint
by converting them into writeable EntityProperties.The API also handles the conversion back to the original complex object from the flattened
EntityProperty
Dictionary. All that the client needs to do is to tell the API, I have thisEntityProperty
Dictionary that I just read from Azure Table (in the form of DynamicTableEntity.Properties), can you convert it to an object of this specific type. The API will recompose the full complex object with all of its properties including 'Enum' properties with their original correct values.All of this flattening and recomposing of the original object is done transparently to the client (user of the API). Client does not need to provide any schema or any knowledge to the ObjectFlattenerRecomposer API about the complex object that it wants to write, it just passes the object to the API as 'object' to flatten it. When converting it back, the client only needs to provide the actual type of object it wants the flattened
EntityProperty
Dictionary to be converted to. The generic ConvertBack method of the API will simply recompose the original object of Type T and return it to the client.See the usage example below. The objects do not need to implement any interface like 'ITableEntity' or inherit from a particular base class either. They do not need to provide a special set of constructors.
Blog: https://doguarslan.wordpress.com/2016/02/03/writing-complex-objects-to-azure-table-storage/
Nuget Package: https://www.nuget.org/packages/ObjectFlattenerRecomposer/
Usage:
Enum is not supported. Even though it is defined like an int, it is really not a integral type supported by Table Storage. Here is the list of types supported. An enum is just a string expression of an integral number with an object-oriented flavor.
You can store int in table storage and then covert it using Enum.Parse.
Here's a simple workaround:
It would have been nicer if a simple backing value could have been employed rather than an additional (public!) property - without the hassle of overriding
ReadEntity
/WriteEntity
of course. I opened a user voice ticket that would facilitate that, so you might want to upvote it.Just suggestions...
I remember that in WCF you have to mark enums with special attributes: http://msdn.microsoft.com/en-us/library/aa347875.aspx
Also, when you declare
PaymentStatuses? PaymentStatus
, you are declaringNullable<PaymentStatuses> PaymentStatus
. The?
sintax is just syntactic sugar. Try to remove the?
and see what happen (you could add a PaymentStatuses.NoSet = 0 , because the default value for an Int32 is 0).Good luck.
ya i was having this same problem i changed my property which was earlier enum to int. now this int property parses the incoming int and saves it into a variale of the same enum type so now the code that was
is chaged to
Parvs solution put me on the right track but I had some minor adjustments.