Azure 存储表 - 不满足更新条件

标签 azure azure-blob-storage

当我尝试更新存储表上的实体时,出现随机异常。我得到的异常(exception)是

System.Data.Services.Client.DataServiceRequestException: An error occurred while processing this request. ---> System.Data.Services.Client.DataServiceClientException: {"odata.error":{"code":"UpdateConditionNotSatisfied","message":{"lang":"en-US","value":"The update condition specified in the request was not satisfied.\nRequestId:2a205f10-0002-013b-028d-0bbec8000000\nTime:2015-10-20T23:17:16.5436755Z"}}} ---

我知道这可能是并发问题,但问题是没有其他进程访问该实体。

有时我会遇到几十个这样的异常,我重新启动服务器,它又开始正常工作了。

public static class StorageHelper
    {
        static TableServiceContext tableContext;

        static CloudStorageAccount storageAccount;

        static CloudTableClient CloudClient;



        static StorageHelper()
        {

                storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
                CloudClient = storageAccount.CreateCloudTableClient();
                tableContext = CloudClient.GetTableServiceContext();
                tableContext.IgnoreResourceNotFoundException = true;
            }



public static void Save(int myId,string newProperty,string myPartitionKey,string myRowKey){
     var entity = (from j in tableContext.CreateQuery<MyEntity>("MyTable")
                          where j.PartitionKey == myId
                          select j).FirstOrDefault();


            if (entity != null)
            {
                entity.MyProperty= myProperty;
                tableContext.UpdateObject(entity);
                tableContext.SaveChanges();


            }
            else
            {
                entity = new MyEntity();
                entity.PartitionKey =MyPartitionKey;
                entity.RowKey =MyRowKey;
                entity.MyProperty= myProperty;
                tableContext.AddObject("MyTable", entity);
                tableContext.SaveChanges();
            }
}

最佳答案

  1. 您发布的代码使用了非常旧的表层,该表层现已过时。我们强烈建议您更新到较新版本的存储库并使用新的表层。看这个StackOverflow question了解更多信息。另请注意,如果您使用的是非常旧版本的存储库,这些库最终将停止工作,因为它们使用的服务版本将在服务端被弃用。

  2. 我们不建议客户像此处那样重复使用 TableServiceContext 对象。它们包含各种可能导致性能问题以及其他不利影响的跟踪。这些限制是我们建议(如上所述)迁移到较新的表层的部分原因。请参阅how-to了解更多信息。

  3. 在表实体上 update operations您必须发送一个指示 etag 的 if-match header 。如果您设置实体的 etag 值,库将为您设置此项。要更新服务上实体的 etag,请使用“*”。

关于Azure 存储表 - 不满足更新条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248955/

相关文章:

javascript - 未知参数类型 '_Bool' EAzureBlobStorageFile 和异常 '*** -[__NSArrayM objectAtIndexedSubscript :]

c# - Azure 共享访问签名 - 签名不匹配

Azure F 系列虚拟机

azure - 在 AKS 中部署 POD 期间应用程序是否会上线?

php - Bing 搜索 API 限制并限制为每个域单个页面

node.js - 将图像上传到 Azure blob 存储

azure-data-factory-2 - Azure 数据工厂 2 : How to split a file into multiple output files

azure - 当函数发布到云时,停止在现有 Blob 上触发 Azure Blob 触发器函数

c# - azure 函数计时器触发器中的 TimerInfo 类有哪些具体细节?

azure - 通过 azure 搜索查询 JSON Blob 存储