Azure cosmosdb Cosmos表API,带有ETag的并发If-Match选项

Azure cosmosdb Cosmos表API,带有ETag的并发If-Match选项,azure-cosmosdb,Azure Cosmosdb,我将CosmosDB表API与Microsoft.Azure.Cosmos.table一起使用,CloudTable对象不提供任何更新/插入操作,以在ETag上使用If匹配头。可用选项如下-如果符合条件,则无一选项提供: public static TableOperation Insert(ITableEntity entity); public static TableOperation Insert(ITableEntity entity, bool echoContent

我将CosmosDB表API与Microsoft.Azure.Cosmos.table一起使用,
CloudTable
对象不提供任何更新/插入操作,以在ETag上使用If匹配头。可用选项如下-如果符合条件,则无一选项提供:

    public static TableOperation Insert(ITableEntity entity);
    public static TableOperation Insert(ITableEntity entity, bool echoContent);
    public static TableOperation InsertOrMerge(ITableEntity entity);
    public static TableOperation InsertOrReplace(ITableEntity entity);
    public static TableOperation Merge(ITableEntity entity);
    public static TableOperation Replace(ITableEntity entity);
如何在更新时实现If匹配约束

PS:尽量避免写一篇文章

如何在更新时实现If匹配约束

有一个名为的属性。对于条件更新,您将需要使用它。如果为ETag值指定
*
,则实体将始终更新。如果指定除
*
以外的任何值,则仅当服务器上实体的ETag值与指定的值匹配时,才会更新实体

更新

因此,我编写了一些示例代码来测试条件更新:

        var account = CloudStorageAccount.Parse(connectionString);
        var tableClient = account.CreateCloudTableClient();
        var table = tableClient.GetTableReference("Test");

        var entity = new MyEntity();
        entity.PartitionKey = "pk4";
        entity.RowKey = "rk4";
        entity.MyIgnoredProperty = "ignored - updated";
        entity.MyOtherProperty = "not ignored - updated";
        entity.ETag = "W/\"0x5B168C7B6E589D2\"";//This is a dummy Etag value to fail the replace operation
        TableOperation replaceOperation = TableOperation.Replace(entity);
        var result = table.Execute(replaceOperation);//Fails with 412 status code
        Console.WriteLine(entity.ToString());
        Console.ReadLine();
并通过Fiddler追踪了请求。如果您注意到,
ETag
值作为
If-Match
请求头传递


上述操作忽略ETag,并使用旧ETag进行写入-已试用并观察到相同情况。我错过什么了吗?这是因为您正在执行的
插入或合并
操作不提供条件更新。如果要强制执行此条件更新,则需要使用
合并
更新
操作。请看这里:。好的,谢谢。因此将需要2个调用-首先插入,如果存在,则合并。当实体不存在时插入更新会更好。但是TableOperation类中没有更新方法:
Replace
SDK中的操作映射到REST API中的
update Entity
操作。对于
Replace
,实体必须存在于表中<如果实体存在,代码>插入将失败。如果不确定实体是否存在,则需要使用
插入或替换
插入或合并
操作。