无法从表存储中获取记录

问题描述 投票:4回答:1

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

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

此外,我已经更改了变量名称,如果您在阅读它们时遇到一些麻烦,请对不起。

c# azure azure-table-storage
1个回答
1
投票

我相信继承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;
    }
}

虽然我仍然相信如果您可以分享有关运行代码时遇到的任何异常的更多详细信息,那将会很棒。有关使用TableStorage的更好解释,请参阅this link

© www.soinside.com 2019 - 2024. All rights reserved.