Azure 表存储 sdk 1.6 Upsert 未插入

发布于 2024-12-22 10:09:39 字数 663 浏览 5 评论 0原文

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

但我只是设法让更新工作。当我尝试更新插入新的行键时,出现资源未找到异常。

 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,能够一次完成这两项任务。

I'm trying to use the new upsert feature of asure sdk 1.6 (against the storage emulator).

But I only managed to get the update working. When I try to upsert whit a new rowkey I get resource not found exception.

 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.
I was thinking being able to do both in one action thanks to the new sdk.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

究竟谁懂我的在乎 2024-12-29 10:09:39

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

it will only work against real Azure storage. Development storage does not support Upsert operation.
Also you must set the IgnoreResourceNotFoundException property of the tableServiceContext to true.

白芷 2024-12-29 10:09:39

我想出了一个似乎适用于 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 之间的区别,可以吗?

提前谢谢你

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?

thank you by advance

如梦 2024-12-29 10:09:39

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

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. Otherwise you will get ResourceNotFound exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文