Azure functions 具有表存储的Azure函数-更新实体不工作

Azure functions 具有表存储的Azure函数-更新实体不工作,azure-functions,azure-table-storage,Azure Functions,Azure Table Storage,我有以下代码: #r "Newtonsoft.Json" #r "Microsoft.WindowsAzure.Storage" using System; using System.IO; using System.Net.Http; using Newtonsoft.Json; using Microsoft.WindowsAzure.Storage.Table; public async static void Run(string myIoTHubMessage, ICollector&

我有以下代码:

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
public async static void Run(string myIoTHubMessage, ICollector<SensorData> tableOutput, CloudTable activityTable,ICollector<SensorData> activityValue, ILogger log)
{
    var data = JsonConvert.DeserializeObject<IoTMessage>(myIoTHubMessage);
    string temp =  data.param2;
    double temperature = double.Parse(temp) * 0.01;

    var sensor = new SensorData { Temperature = temperature.ToString(),DeviceId = data.deviceId, RowKey = data.messageId, PartitionKey = data.deviceId };
    tableOutput.Add(sensor);

    var query = new TableQuery<SensorValue>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, data.deviceId.ToString()));
    var segment = await activityTable.ExecuteQuerySegmentedAsync(query, null);

    if (segment.Results.Count == 0)
    {
        activityValue.Add(sensor);
    }
    else
    {
        SensorValue sensorValue = new SensorValue
        {
            PartitionKey = data.deviceId,
            RowKey = data.messageId,
            Temperature = Convert.ToInt16(temperature),
        };

        var operation = TableOperation.Replace(sensorValue);
        await activityTable.ExecuteAsync(operation);
        log.LogInformation(segment.Results.Count.ToString());
    }
}
public class SensorData
{
    public string RowKey { get; set; }
    public string PartitionKey { get; set; }
    public string Temperature { get; set; }
    public string DeviceId {get; set;}
}

public class IoTMessage
{
    public string messageId { get; set; }
    public string temperature { get; set; }
    public string deviceId { get; set; }

}

public class SensorValue : TableEntity
{
    public string RowKey { get; set; }
    public string PartitionKey { get; set; }
    public string Temperature { get; set; }
    public string DeviceId { get; set; }
}
在activityTable表中没有此类设备的情况下,记录本身可以正常工作,没有任何问题,不幸的是,如果有此类设备不工作,则更新,即没有错误,但也没有记录更新:(


在我的例子中,在activityTable中,请求中发送了deviceId表的PartitionKey。我尝试从早上8点开始执行此操作,但没有任何效果,我无法处理它:(

快速解决方案:将
ETag
字段设置为
*
,正如您在Azure提示和技巧文章中看到的那样

稍长的版本:根据表存储,您试图替换的实体不存在,因为您省略了
ETag

ETag属性用于更新期间的乐观并发。它不是时间戳,因为有另一个名为timestamp的属性存储上次更新记录的时间。例如,如果加载实体并要更新它,则ETag必须与当前存储的内容匹配。如果您有多个用户编辑同一项,您不希望他们覆盖彼此的更改


将ETag设置为
*
实际上是将其设置为可接受任何值的通配符。

快速解决方案:将
ETag
字段设置为
*
,如Azure提示和技巧文章所示

稍长的版本:根据表存储,您试图替换的实体不存在,因为您省略了
ETag

ETag属性用于更新期间的乐观并发。它不是时间戳,因为有另一个名为timestamp的属性存储上次更新记录的时间。例如,如果加载实体并要更新它,则ETag必须与当前存储的内容匹配。如果您有多个用户编辑同一项,您不希望他们覆盖彼此的更改

将ETag设置为
*
实际上是将其设置为接受任何值的通配符

   if (segment.Results.Count == 0)
        {
            activityValue.Add(sensor);
        }
        else
        {
            SensorValue sensorValue = new SensorValue
            {
                PartitionKey = data.deviceId,
                RowKey = data.messageId,
                Temperature = Convert.ToInt16(temperature),
            };

            var operation = TableOperation.Replace(sensorValue);
            await activityTable.ExecuteAsync(operation);
            log.LogInformation(segment.Results.Count.ToString());
        }