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
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)
currencyEntity
you passed intoInsertNewCurrencyEntityAsync
method? – Ivan Yang