是否可以对 CosmosDB 集合使用继承?



我是了解NoSQL数据库的新手。

假设我有一个层次结构,例如

public interface MyInterface
{
[JsonProperty("id")]
Guid Id { get; }
}
public class Foo : MyInterface
{
public Guid Id { get; set; }
[JsonProperty("intVal")]
public int IntVal { get; set; }
}
public class Bar : MyInterface
{
public Guid Id { get; set; }
[JsonProperty("stringVal")]
public string StringVal { get; set; }
}

是否可以创建一个包含MyInterfaces的集合?或者它只能保存固定的 Json 结构,我必须做类似的事情

public class MyClass
{
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter))]
public MyClassType Discriminator { get; set; }
[JsonProperty("fooIntVal")]
public int? FooIntVal { get; set; }
[JsonProperty("barStringVal")]
public string BarStringVal { get; set; }
}
public Enum MyClassType
{
[JsonProperty("foo")]
Foo,
[JsonProperty("bar")]
Bar
};

???

是否可以创建一个包含 MyInterfaces 的集合?

当然可以。如注释中所述,cosmos db 集合没有架构,因此你可以将其设计为要设计的任何结构。

如:

{
"id": "1",
"Discriminator": "foo",
"FooIntVal": 5
},
{
"id": "2",
"Discriminator": "bar",
"BarStringVal": "aaa"
}

甚至:

{ 
"id": "5",
"Discriminator": {
"foo": {
"FooIntVal": 5
},
"bar": {
"BarStringVal": "aaa"
}
}
}

您可以灵活调整数据结构。我建议您参考此文档以开始使用。如有任何疑问,请告诉我。

最新更新