2
votes

I am trying to Replace an entity on my table based on Partition Key and Row key retrieve the entity successfully but when i try to cast it, it throws an invalid cast exception. I looked at the MSDN docs and that is the correct way to delete, even made sure to follow the guidelines for Creating an entity

Entity properties you'd like to store in a table must be public properties of the type, and support both getting and setting of values. Also, your entity type must expose a parameter-less constructor

This is my class

    public class BasicAsset : TableEntity
{
    public BasicAsset()
    {
    }

    public BasicAsset(string name)
    {
        Name = name;
    }


    [IsFilterable, IsSortable, IsSearchable]
    public string Name { get; set; }

    [IsFilterable, IsSortable]
    public int Version { get; set; }
}

And this is my code at the point of the exception

TableOperation retreiveOperation = TableOperation.Retrieve("Orginization", result.Results[0].Document.RowKey);
TableResult toDelete = await table.ExecuteAsync(retreiveOperation);
BasicAsset toReplaceAsset = (BasicAsset) toDelete.Result;
//Change what is new here
toReplaceAsset.Version = asset.Version;
TableOperation replaceOperation = TableOperation.Replace(toReplaceAsset);

The error

   e = {System.InvalidCastException: Unable to cast object of type 'Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity' to type 'AssetSynch.Models.BasicAsset'.
   at AssetSynch.Controllers.TableStorageViewFunctions.<>c__DisplayClass0_0.<<UpdateLattestAssetVe...

What am i missing here?

1

1 Answers

12
votes

Instead of Retrieve try using Retrieve<BasicAsset>, alternatively you could simply call ExecuteQuery<BasicAsset>