c# - 无法将动态表实体转换为自定义实体 Azure 表存储

标签 c# azure azure-table-storage

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

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

这是我的课

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

我在这里缺少什么?

最佳答案

而不是 Retrieve尝试使用 Retrieve<BasicAsset> ,或者您也可以直接调用 ExecuteQuery<BasicAsset>

关于c# - 无法将动态表实体转换为自定义实体 Azure 表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053834/

相关文章:

c# - 连接来自 IQueryable 和 IList<KeyValuePair> 的数据

c# - Enumerable.Intersperse 的扩展方法?

python - 使用 Azure 认知服务读取 gif 图像中的文本

asp.net - 错误: XML transformation error in Azure Pipelines

azure - 根据行数或大小选择 Azure 表分区键

Azure 表存储查询唯一分区键

c# - 当前上下文中不存在名称 'f_name'

c# - 从线程单线程单元线程中获取资源/内存

sql-server - 为什么我的客户端IP地址每次显示不同的IP?

c# - 这是简化继承类的有效方法吗?