无法保存 Nullable当 Azure 表为空时

发布于 2024-12-27 03:01:50 字数 954 浏览 5 评论 0原文

例如,我有一个 bool? C#中的Status

Status = true/false时,它可以保存到Azure表,没问题

但是当Stats = null时,它无法保存(更新)到Azure表,该列仍然保留旧值

我想这可能是因为Azure表没有方案,但是解决方案是什么?

如何保存null覆盖原始值?

编辑,代码

数据如下:

public class someEntity : TableServiceEntity
{
    public bool? Status { get; set; }
}

更新如下:(

tableContext.AttachTo("sometable", someEntity);
tableContext.UpdateObject(someEntity);
tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch & SaveChangesOptions.ReplaceOnUpdate);

我尝试将 AttachTo"*" 作为 etag,尝试删除 SaveChangesOptions,都不起作用)

对不起我的愚蠢,应该是这段代码,然后就可以了

tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);

For example, I have a bool? Status in c#

when the Status = true/false, it can save to Azure table, no problem

But when Stats = null, it cannot save(update) to Azure table, the column still keeps the old value

I guess it might because Azure table does not have a scheme, but whats the solution?

How to save a null to overwrite the original value?

EDIT, the code

data like this:

public class someEntity : TableServiceEntity
{
    public bool? Status { get; set; }
}

update like this:

tableContext.AttachTo("sometable", someEntity);
tableContext.UpdateObject(someEntity);
tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch & SaveChangesOptions.ReplaceOnUpdate);

(I tried AttachTo with "*" as etag, tried remove SaveChangesOptions, neither work)

SORRY for my stupid, should be this code, then works

tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);

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

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

发布评论

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

评论(1

凶凌 2025-01-03 03:01:50

正如@Matthew建议的,我把答案放在这里

非常简单,使用:

tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);

对于多个选项,使用 | (或),而不是 & (和)原因

SaveChangesOptions.ReplaceOnUpdate = true 表示整个实体将被替换
SaveChangesOptions.ReplaceOnUpdate = false,表示合并,旧数据将保留

As @Matthew suggested, I put the answer here

very simple, use:

tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);

For multiple options, use | (or), not & (and). Reason

SaveChangesOptions.ReplaceOnUpdate = true means the whole entity will be replaced
SaveChangesOptions.ReplaceOnUpdate = false, means merge, the old data will be kept

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