Azure表存储插入,带有C#未插入到表中

Azure表存储插入,带有C#未插入到表中,c#,azure,azure-table-storage,C#,Azure,Azure Table Storage,我是Azure Table Storage的新手,学习了许多教程,但无法获得任何代码来将数据插入表中。实际上,我现在使用Azure Storage Explorer导入包含数据的csv文件来填充表。我可以使用以下代码从这些表中检索数据 public async Task<CurrencyEntity> GetCurrencyDataForDate(string currency, string date) { //ensure variables match

我是Azure Table Storage的新手,学习了许多教程,但无法获得任何代码来将数据插入表中。实际上,我现在使用Azure Storage Explorer导入包含数据的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 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;

    }
公共异步任务InsertNewCurrencyEntityAsync(CurrencyEntity CurrencyEntity)
{
//确保值的格式正确
currencyEntity.Currency=currencyEntity.Currency.ToUpper();
var parts=currencyEntity.Date.Split(“-”);
如果(parts.Length!=3)返回null;
if(parts[0].Length!=4 | | parts[1].Length!=2 | | parts[2].Length!=2)返回null;
表格结果;
var insertOrMergeOperation=TableOperation.InsertOrMerge(currencyEntity);
var table=GetCloudTable(CcdConn,TableName);
//执行该操作。
尝试
{
结果=wait table.ExecuteAsync(insertOrMergeOperation);
}
捕获(例外e)
{
//值不能为null。(参数“Upserts需要有效的PartitionKey”)。详细信息如下:
var message=$“{e.message}详细信息为:{e.InnerException}”;
投掷;
}
//var result=wait table.ExecuteAsync(insertOrMergeOperation);
var newCurrency=(CurrencyEntity)result.result;
返回新货币;
}
它返回如下所示的新货币

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

我认为问题是因为您实际使用的是
InsertOrMerge
方法,而不是
InsertNewCurrencyEntityAsync(CurrencyEntity CurrencyEntity)
函数中的
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

 }
公共异步任务InsertNewCurrencyEntityAsync(CurrencyEntity CurrencyEntity)
{
//其他代码
表格结果;
//var insertOrMergeOperation=TableOperation.InsertOrMerge(currencyEntity);
//如果要添加新记录,请使用Insert方法。
var insertOrMergeOperation=TableOperation.Insert(currencyEntity);
var table=GetCloudTable(CcdConn,TableName);
//其他代码
}

这似乎与您想要做的类似,唯一的区别是您没有获得表引用名称。表引用名称是callusing Microsoft.Azure.Cosmos.table中的TableName变量;但是Azure.Storage.Table sdk无法用于插入或检索。。。我现在可以找回了,我只是无法插入。这是一个Azure存储表。@dinotom,你介意分享你传入
InsertNewCurrencyEntityAsync
方法的
currencyEntity
的真实值吗?@dinotom,有错误吗?最好提供插入的详细值。您也可以在运行此代码时打开fiddler,查看插入操作是否有相关的
http Post请求
。@dinotom,您好,我想知道是否有此问题的更新?我做过肩部手术,所以一直没有工作。现在我已经准备好了,我今天晚些时候会对此进行检查。我发现了这个问题,当我测试它时,我使用的是小写货币字符串,实体搜索没有将其转换为大写。我在CurrencyEntity构造函数中修复了这个问题。现在一切都好了。
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

 }