【问题标题】:Failed to cast Dynamic Table entity to custom Entity Azure Table storage无法将动态表实体转换为自定义实体 Azure 表存储
【发布时间】:2017-10-18 15:02:41
【问题描述】:

我正在尝试根据分区键和行键替换表上的实体,成功检索实体,但是当我尝试强制转换它时,它会引发无效的强制转换异常。 我查看了 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; }
}

这是我发生异常时的代码

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);

错误

   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...

我在这里错过了什么?

【问题讨论】:

    标签: c# azure azure-table-storage


    【解决方案1】:

    请尝试使用Retrieve&lt;BasicAsset&gt;,而不是Retrieve,或者您可以直接调用ExecuteQuery&lt;BasicAsset&gt;

    【讨论】:

      猜你喜欢
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2016-09-19
      • 2011-05-26
      相关资源
      最近更新 更多