1

我正在尝试使用 asure sdk 1.6 的新 upsert 功能(针对存储模拟器)。

但我只设法让更新工作。当我尝试更新一个新的行键时,我得到了resource not found异常。

 var context = new TableServiceContext(_cloudStorageAccount.TableEndpoint.ToString(), _cloudStorageAccount.Credentials)
            {
                MergeOption = MergeOption.NoTracking,
                ResolveType = (unused) => typeof(SmartTableServiceEntity)
            };
context.AttachTo(tableName, smartEntity, "*");
            context.UpdateObject(smartEntity);
            context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);

如果我把AddObject它插入而不是更新。多亏了新的 sdk,我在想能够在一个动作中做到这两点。

4

3 回答 3

7

它只适用于真正的 Azure 存储。开发存储不支持 Upsert 操作。此外,您必须将 tableServiceContext 的 IgnoreResourceNotFoundException 属性设置为 true。

于 2011-12-21T11:55:07.740 回答
3

我想出了一个似乎适用于 devstorage 和真实存储的解决方案

var context = CreateNewContext();

            context.IgnoreResourceNotFoundException = true;

            if (context.StorageCredentials.AccountName == "devstoreaccount1")
            {
                var entityCheck = context.CreateQuery<SmartTableServiceEntity>(tableName)
                    .Where(e => e.PartitionKey == partitionKey && e.RowKey == rowKey).FirstOrDefault();

                if (entityCheck == null) {
                    context.AddObject(tableName, smartEntity);
                }
                else  {
                    context.Detach(entityCheck);
                    context.AttachTo(tableName, smartEntity, "*");
                    context.UpdateObject(smartEntity);
                }
            }
            else 
            {
                context.AttachTo(tableName, smartEntity, null);
                context.UpdateObject(smartEntity);
            }
            context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);

有人有更好的解决方案吗?注意“*”和 null 之间的区别可以吗?

提前谢谢你

于 2011-12-21T17:49:43.793 回答
0

使用真实的 Azure 帐户时,以及将 IgnoreResourceNotFoundException 设置为 true,为 eTag 参数传递 null 或使用不接受 eTag 值的重载也很重要。否则你会得到 ResourceNotFound 异常。

于 2012-01-23T13:41:08.873 回答