简体   繁体   English

不插入Azure Table Storage SDK 1.6 Upsert

[英]Azure Table Storage sdk 1.6 Upsert not inserting

I'm trying to use the new upsert feature of asure sdk 1.6 (against the storage emulator). 我正在尝试使用asure sdk 1.6的新upsert功能(针对存储模拟器)。

But I only managed to get the update working. 但是我只设法使更新生效。 When I try to upsert whit a new rowkey I get resource not found exception. 当我尝试使用新的行键更新时,我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);

If I put AddObject it does the insert but not the update. 如果我把AddObject放进去,而不是更新。 I was thinking being able to do both in one action thanks to the new sdk. 多亏了新的SDK,我一直想能够一举两得。

it will only work against real Azure storage. 它仅适用于实际的Azure存储。 Development storage does not support Upsert operation. 开发存储不支持Upsert操作。 Also you must set the IgnoreResourceNotFoundException property of the tableServiceContext to true. 另外,您必须将tableServiceContext的IgnoreResourceNotFoundException属性设置为true。

I came up this a solution that seems to works on devstorage and on real storage 我想出了一个似乎可以在存储和实际存储上使用的解决方案

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);

does someone has a better solution? 有人有更好的解决方案吗? notice the difference bettween "*" and null is it ok? 请注意“ *”和null之间的区别可以吗?

thank you by advance 预先谢谢你

When using the real Azure accounts, as well as setting the IgnoreResourceNotFoundException to true, it is also important to pass null for the eTag parameter, or use the overload that does not accept the eTag value. 使用真实的Azure帐户以及将IgnoreResourceNotFoundException设置为true时,为eTag参数传递null或使用不接受eTag值的重载也很重要。 Otherwise you will get ResourceNotFound exceptions. 否则,您将获得ResourceNotFound异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM