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

Is it possible to use inheritance with CosmosDB collections?

我刚开始了解 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
};

???

Is it possible to create a collection that holds MyInterfaces?

当然可以。如评论中所述,cosmos db 集合没有模式,因此您可以将其设计成您想要设计的任何结构。

如:

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

甚至:

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

你可以调整数据结构flexibly.I建议你参考这个doc开始。如有任何问题,请告诉我。