gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:13:23 24 4
gpt4 key购买 nike

当我尝试更新存储表上的实体时,出现随机异常。我得到的异常(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com