【问题标题】:Azure Table Storage insert with C# not inserting into table带有 C# 的 Azure 表存储插入未插入到表中
【发布时间】:2021-02-22 12:45:51
【问题描述】:

我是 Azure 表存储的新手,学习了许多教程,但无法获取任何代码来将数据插入表中。实际上,我现在使用 Azure 存储资源管理器通过导入包含数据的 csv 文件来填充表。我可以使用以下代码从这些表中检索数据。

public async Task<CurrencyEntity> GetCurrencyDataForDate(string currency, string date)
    {
        //ensure variables match key formats
        currency = currency.ToUpper();
        //var stringDate = date.ToString("yyyy-MM-dd");
        
        var table = GetCloudTable(CcdConn, TableName);
        var retrieveOperation = TableOperation.Retrieve<CurrencyEntity>(currency, date);
        var result = await table.ExecuteAsync(retrieveOperation);

        var dto =  result?.Result as CurrencyEntity;
        return dto;
    }

CurrencyEntity 在哪里

public class CurrencyEntity : TableEntity
{
    public CurrencyEntity() { }
    public CurrencyEntity(string currency, string date)
    {
        Currency = currency;
        Date = date;

        PartitionKey = currency;
        RowKey = date;
    }
    public CurrencyEntity(string currency, string date, string close)
    {
        Currency = currency;
        Date = date;
        Close = close;

        PartitionKey = currency;
        RowKey = date;
    }

    public string Currency { get; set; }
    public string Date { get; set; }
    public string Close { get; set; }
    
}

但是这种方法,虽然一直运行到完成并返回 CurrencyEntity 却没有

 public async Task<CurrencyEntity> InsertNewCurrencyEntityAsync(CurrencyEntity currencyEntity)
    {
        //ensure values have proper format
        currencyEntity.Currency = currencyEntity.Currency.ToUpper();
        var parts = currencyEntity.Date.Split("-");
        if (parts.Length != 3) return null;
        if (parts[0].Length != 4 || parts[1].Length != 2 || parts[2].Length != 2) return null;

        TableResult result;
        var insertOrMergeOperation = TableOperation.InsertOrMerge(currencyEntity);
        var table = GetCloudTable(CcdConn, TableName);
        // Execute the operation.
        try
        {
            result = await table.ExecuteAsync(insertOrMergeOperation);
        }
        catch (Exception e)
        {
            //Value cannot be null. (Parameter 'Upserts require a valid PartitionKey') The details are: 
            var message = $"{e.Message} The details are: {e.InnerException}";
            throw;
        }
        //var result = await table.ExecuteAsync(insertOrMergeOperation);
        var newCurrency = (CurrencyEntity)result.Result;

        return newCurrency;

    }

它返回的 newCurrency 看起来像这样

即使没有异常,错误的 Timestamp 属性也会给出一些失败的指示。 检索此数据时,它返回 null(未插入)

【问题讨论】:

  • 这似乎与您想要做的类似,我注意到的唯一区别是您没有获得表引用名称。 serversncode.com/…
  • 表引用名是调用中的TableName变量
  • 使用 Microsoft.Azure.Cosmos.Table;但是 Azure.Storage.Table sdk 不适用于插入或检索......我现在可以检索,我只是无法插入。这是一个 Azure 存储表。
  • @dinotom,你介意分享你传递给InsertNewCurrencyEntityAsync方法的currencyEntity的真正价值吗?

标签: c# azure azure-table-storage


【解决方案1】:

我认为问题是由于您实际上在 InsertNewCurrencyEntityAsync(CurrencyEntity currencyEntity) 函数中使用了 InsertOrMerge 方法而不是 Insert 方法。

如果要插入新记录,请使用Insert方法,如下所示:

public async Task<CurrencyEntity> InsertNewCurrencyEntityAsync(CurrencyEntity currencyEntity)
{

   //other code

    TableResult result;

    //var insertOrMergeOperation = TableOperation.InsertOrMerge(currencyEntity);

    //use the Insert method if you want to add a new record.
    var insertOrMergeOperation = TableOperation.Insert(currencyEntity);

    var table = GetCloudTable(CcdConn, TableName);

   
  //other code

 }

【讨论】:

  • @dinotom,有什么错误吗?最好提供您插入的详细值。
  • 运行这段代码的时候也可以打开fiddler,看看有没有相关的http Post request进行插入操作。
  • @dinotom,你好,我想知道这个问题是否有任何更新?
  • 我做了肩部手术,所以一直没有工作。既然我已经决定了,今天晚些时候会回顾一下
  • 我发现了这个问题,当我测试它时,我使用的是小写货币字符串,它没有被转换为实体搜索的大写。我在 CurrencyEntity 构造函数中修复了这个问题。现在一切都好。
猜你喜欢
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 2021-12-30
  • 2021-11-26
  • 2023-03-16
  • 2014-05-03
  • 1970-01-01
  • 2021-01-09
相关资源
最近更新 更多