Azure Table 存储插入 C# 未插入 table

Azure Table Storage insert with C# not inserting into table

我是 Azure Table 存储的新手,并遵循了一些教程,但无法使用任何代码将数据插入 table。实际上,我现在使用 Azure 存储资源管理器通过导入包含数据的 csv 文件来填充 tables。我可以使用以下代码从那些 table 中检索数据。

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;

    }

它returns newCurrency 看起来像这样

不正确的时间戳 属性 给出了一些失败的迹象,即使没有例外。 检索此数据时,它 returns 为空(未插入)

我认为问题是由于您实际上在 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

 }