Saravanan
Saravanan

Reputation: 930

Azure TableEntity Flatten method not working for "Byte" data type

While trying to insert data object into azure-storage-table, TableEntity.Flatten is throwing below exception.

System.Runtime.Serialization.SerializationException: Unsupported type : System.Byte encountered during conversion to EntityProperty.

Data object contains byte property which is not supported. As I receive this data object from upstream, I am ending up with new class, copying all the properties while changing the byte property into int.

Is there any other better alternative to it?

Upvotes: 0

Views: 878

Answers (2)

Dogu Arslan
Dogu Arslan

Reputation: 3384

Byte array is a supported property but you are right that byte is not. The types that are supported are here, check the method that does the conversion. https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/Table/EntityPropertyConverter.cs

A workaround could be to convert the byte property to byte array of a single element. But I strongly reccomend you to open a bug in github to add byte support. It should be easy to add this.

Sent out a pull request to add byte property support to Flatten/ConvertBack methods: https://github.com/Azure/azure-storage-net/pull/537/files

While waiting above pull request to be merged into the SDK. I have gone ahead and updated the original nuget package I wrote to support byte type and IEnumerable properties, it is here: https://www.nuget.org/packages/ObjectFlattenerRecomposer/

You should be able to use that as well. the methods are the same Flattena dn ConvertBack but they support Byte and all other IEnumerable types.

Update: This is now fixed in latest in latest version of the SDK. You still need to use Flatten and ConvertBack methods but you would be able to write and read byte type properties using the latest version.

Upvotes: 2

Zhaoxing Lu
Zhaoxing Lu

Reputation: 6467

You can consider converting byte to an int property in the same class, and mark the byte property with [IgnoreProperty] attribute.

public class MyEntity : TableEntity
{
    public int MyPropertyInt { get; set; }

    [IgnoreProperty]
    public byte MyProperty
    {
        get
        {
            return (byte)this.MyPropertyInt;
        }

        set
        {
            this.MyPropertyInt = value;
        }
    }
}

Upvotes: 0

Related Questions