0
votes

I am new to Azure Table Storage, and followed a number of tutorials but cannot get any code to work to insert data into the table. I actually populated the tables for now using Azure Storage Explorer by importing the csv files that have the data. I can retrieve the data from those tables using the following code.

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

Where CurrencyEntity is

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

But this method, although running thru to completion and returning a CurrencyEntity does not

 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;

    }

It returns newCurrency which looks like this

enter image description here

The incorrect Timestamp property gives some indication of failure even though there is no exception. When retrieving this data it returns null (it wasn't inserted)

1
this seems similar to what you want to do, only difference i notice is you dont get the table reference name. serversncode.com/…rafael gonzalez
the table reference name is the TableName variable in the calldinotom
using Microsoft.Azure.Cosmos.Table; but the Azure.Storage.Table sdk did not work for inserting or retrieving... I can retrieve now, I just cant insert. This is an Azure Storage Table.dinotom
@dinotom, do you mind sharing the real value of currencyEntity you passed into InsertNewCurrencyEntityAsync method?Ivan Yang

1 Answers

0
votes

I believe the issue is due to that you are actually using InsertOrMerge method instead of Insert method in InsertNewCurrencyEntityAsync(CurrencyEntity currencyEntity) function.

Please use Insert method if you want to insert a new record, like below:

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

 }