当我尝试更新存储表上的实体时,出现随机异常。我得到的异常(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();
}
}
最佳答案
您发布的代码使用了非常旧的表层,该表层现已过时。我们强烈建议您更新到较新版本的存储库并使用新的表层。看这个StackOverflow question了解更多信息。另请注意,如果您使用的是非常旧版本的存储库,这些库最终将停止工作,因为它们使用的服务版本将在服务端被弃用。
我们不建议客户像此处那样重复使用 TableServiceContext 对象。它们包含各种可能导致性能问题以及其他不利影响的跟踪。这些限制是我们建议(如上所述)迁移到较新的表层的部分原因。请参阅how-to了解更多信息。
在表实体上 update operations您必须发送一个指示 etag 的 if-match header 。如果您设置实体的 etag 值,库将为您设置此项。要更新服务上实体的 etag,请使用“*”。
关于Azure 存储表 - 不满足更新条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248955/