c# - 无法从表存储中获取记录

标签 c# azure azure-table-storage

我有一个表存储表,我想从中获取一些数据。插入和更新查询工作正常,但当我尝试选择一些记录时遇到问题。下面是我到目前为止所做的代码:

class TransactionEntity : TableEntity
{
    public String s{ get; set; }
    public Int32 r { get; set; }
    public String e{ get; set; }
    public String t{ get; set; }
    public String l{ get; set; }
    public TransactionEntity(String id, String s, String e, String t)
    {
        this.r= 0;
        this.s= s;
        this.RowKey = id;
        this.PartitionKey = Guid.NewGuid().ToString();
        this.e= e == null ? "" : e;
        this.t= t== null ? "" : t;
    }
}

管理表的代码:

class TableStorageManager
{
    public Boolean AddTransaction(TransactionEntity dto)
    {
        try
        {
            // Create the table client.
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));
            table.CreateIfNotExists();
            TableOperation op = TableOperation.Insert(dto);    
            table.Execute(op);    
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public List<TransactionEntity> RetrieveAllFailedTransactions()
    {
        try
        {
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));         
            TableQuery<TransactionEntity> query = new TableQuery<TransactionEntity>().Where("s eq '" + ConfigurationManager.AppSettings.Get("E") + "' and r lt " + ConfigurationManager.AppSettings.Get("M") + "");
            query.Take(ConfigurationTasks.GetResultLength());  
            return table.ExecuteQuery(query).ToList();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public Boolean UpdateTransactionStatus(TransactionEntity dto)
    {
        try
        {                    
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));                   
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));
            dto.ETag = "*";
            TableOperation op = TableOperation.Replace(dto);
            table.Execute(op);    
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

此外,我还更改了变量名称,如果你们在阅读它们时遇到一些麻烦,我很抱歉。

最佳答案

我相信继承 TableEntity 的类中可能缺少默认/无参数构造函数。当从 TableStorage 接收对象时,非常需要无参数构造函数来反序列化对象。

因此,将您的 TableEntity 代码更改为:

class TransactionEntity : TableEntity
{
    public String s{ get; set; }
    public Int32 r { get; set; }
    public String e{ get; set; }
    public String t{ get; set; }
    public String l{ get; set; }
    public TransactionEntity(){//do nothing}
    public TransactionEntity(String id, String s, String e, String t)
    {
        this.r= 0;
        this.s= s;
        this.RowKey = id;
        this.PartitionKey = Guid.NewGuid().ToString();
        this.e= e == null ? "" : e;
        this.t= t== null ? "" : t;
    }
}

尽管我仍然相信,如果您能分享有关运行代码时遇到的任何异常的更多详细信息,那就太好了。 另请参阅this link更好地解释如何使用 TableStorage。

关于c# - 无法从表存储中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55608767/

相关文章:

python - 将 Windows Azure 辅助角色与 Celery 结合使用

logging - 使用辅助角色azure解析iis日志

javascript - 尝试在 JavaScript 中从 Azure 表存储获取值时出错

c# - 安装 MySQL Connector .NET 后 ASP.Net MVC4 配置错误

c# - 将新项目添加到列表时,ListView ItemSsource 和 RaisePropertyChanged 不起作用

javascript - 未根据 OPTIONS 请求发送 Cookie

node.js - 为什么这个Azure时间触发功能不会失败?

azure-table-storage - 如何在 Azure 表中检索具有相同分区键的多个条目?

c# - 从 C# 访问 ListBox 的 ScrollViewer

c# - 如何在C#中使用定界符?