DocumentDB Upsert 和 Replace 不改变复杂的属性?

DocumentDB Upsert and Replace not altering complex properties?

Using "Microsoft.Azure.DocumentDB": "1.5.2" 我在将复杂对象正确保存到数据库时遇到问题。他们的初始存储似乎没问题,但是当仅对复杂结构的较深部分进行更改时,Upsert 或 Replace 似乎无法正确更新文档。

我有一个看起来像这样的对象(有些改动,缩减版本)...

public class Profile : Document    {
        public Profile()
        {
            Id = Guid.NewGuid().ToString();
        }

        [JsonProperty(PropertyName = "userId")]
        public string UserId { get; set; }

        [JsonProperty(PropertyName = "defaultContext")]
        public Guid DefaultContext { get; set; }

        [JsonProperty(PropertyName = "contexts")]
        public Dictionary<Guid,UserContext> Contexts { get; set; }
    }
}

UserContext 看起来像这样...

public class UserContext
{
        [JsonProperty(PropertyName = "flag")]
        public Boolean Flag { get; set; }

        [JsonProperty(PropertyName = "stuff")]
        public List<String> Stuff { get; set; }
}

想法是一个配置文件可以有多个上下文,但有一个默认上下文。这让我可以说...

userProfile.Contexts[userprofile.DefaultContext].Flag = true;

问题是,假设其中一个存在于 Azure DocumentDB 中,如果我在上下文中更改 "flag" ReplaceDocumentAsync(userProfile) 和 UpsertDocumentAsync(collectionUri, userProfile) 实际上似乎都不会将更改保存到数据库。

例如在这样一个简单的例子中...

userProfile.Contexts[userprofile.DefaultContext].Flag = true;    
var result = await Client.ReplaceDocumentAsync(userProfile);

我可以在调试器中查看 userProfile,发现 Flag 设置为 true。然后如果我看结果,Flag 仍然是假的(它是操作前数据库中的值)。

我已经用 "strong" 和 "session" 一致性标志试过了,但没有成功。我觉得 class 的定义中有些东西阻止它正确地序列化到 DocumentDB,但我不确定那可能是什么。

欢迎提出任何想法:)

这是 JSON.NET 的一个奇怪的序列化问题。 您正在扩展的文档是一个动态对象(这意味着您可以在运行时向对象添加新属性等)。 JSON.NET 在混合动态和静态对象(如 Dictionary<>

时,有一种有趣的处理序列化的方法

解决此问题的最佳方法是不从 Document 扩展并使用简单的 POCO。 一旦你这样做,问题就会消失。

将您的 class 更改为: public class 简介:{ ... }