gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:03 27 4
gpt4 key购买 nike

我试图根据分区键和行键成功检索实体,但当我尝试转换它时,它会抛出无效的转换异常。我查看了 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com