无法将动态表实体强制转换为自定义实体Azure表存储

Har*_*rry 1 c# azure azure-table-storage

我试图基于分区键替换我的表上的实体和行键成功检索实体但当我尝试强制转换它时,它会抛出一个无效的强制转换异常.我查看了MSDN文档,这是正确的删除方法,甚至确保遵循创建实体的准则

您要存储在表中的实体属性必须是该类型的公共属性,并支持获取和设置值.此外,您的实体类型必须公开无参数构造函数

这是我的班级

    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; }
}
Run Code Online (Sandbox Code Playgroud)

这是我在异常时的代码

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);
Run Code Online (Sandbox Code Playgroud)

错误

   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...
Run Code Online (Sandbox Code Playgroud)

我在这里失踪了什么?

Dog*_*lan 8

而不是Retrieve尝试使用Retrieve<BasicAsset>,或者你可以简单地打电话ExecuteQuery<BasicAsset>