保存时更改/控制CosmosDb文档中的属性顺序



这可能看起来很傻,但很烦人。我使用C#和cosmosdb sdk

我在数据库中有一个文档,比如employee,在我的代码中还有一个相应的employee类。

employee
{
id: "123",
firstName : "Hans",
age : 23
}

以及当将代码中的Employee类更新为时

public class Employee
{
public string Id{get;set}
public string FirstName{get;set}
public string LastName{get;set} // new
public int Age{get;set}
public Dictionary<string, object> OtherProperties {get;set} // also new
}

然后读取代码(GetById(中的现有文档,然后再次上传我的文档,看起来像这个

employee
{  
otherProperties : null,
id: "123"
firstName : "Hans",
lastName : null,
age : 23
}

让我恼火的是otherProperties-属性现在是第一个。lastName(也被添加(恰好在类中的位置。我能以某种方式解决这个问题吗?

我已经测试过在类中以另一种顺序使用该属性,并尝试将数据类型更改为字符串,没有区别。

您能从函数的角度解释一下属性的顺序会产生什么问题吗?

您可以在JsonProperty上使用Newtonsoft.Json的Order来装饰类:https://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm但除了外观上的效果外,功能上也没有太大区别。

类似于:

public class Employee
{
[JsonProperty(Order = 1)]
public string Id{get;set}

[JsonProperty(Order = 2)]
public string FirstName{get;set}

[JsonProperty(Order = 3)]
public string LastName{get;set}

[JsonProperty(Order = 4)]
public int Age{get;set}

[JsonProperty(Order = 5)]
public Dictionary<string, object> OtherProperties {get;set}
}

最新更新