[英]Mongo C# inherited list
I have a class (a) which inherits a list of (b). 我有一个类(a)继承了(b)的列表。
public class a : List<b>
{
}
Now when I try to save an object of this class to Mongo I get an error. 现在,当我尝试将此类的对象保存到Mongo时,出现错误。
Serializer EnumerableSerializer<b> expected serialization options of type ArraySerializationOptions, not DocumentSerializationOptions.
Serializer EnumerableSerializer <b>预期为ArraySerializationOptions类型的序列化选项,而不是DocumentSerializationOptions类型。
Have you ever come across anything like this, if anyone could shed some light that would be great. 您是否曾经遇到过类似的事情,如果有人可以阐明一些很棒的想法。 I can't find much in the documentation or online about this.
我在文档或在线中找不到太多关于此的内容。
The top level document being saved to MongoDB cannot be a list, it has to be a class. 保存到MongoDB的顶级文档不能是列表,而必须是一个类。 It also has to have an Id field.
它还必须具有一个ID字段。 So you could do something like this:
因此,您可以执行以下操作:
public class C
{
public ObjectId Id { get; set; }
public A List { get; set; }
}
public class A : List<B>
{
}
Where you have encapsulated the list of type A inside a document (in this case of type C). 您已将A类型的列表封装在文档中(在这种情况下为C类型)。
However, be careful if you decide to keep your class of type A. If you add custom properties to your class A you will then have to write a custom serializer to ensure that the custom values you added are properly serialized along with the list values. 但是,如果决定保留类型A的类,请务必小心。如果将自定义属性添加到类A,则必须编写一个自定义序列化程序,以确保添加的自定义值与列表值一起正确序列化。 See:
看到:
https://jira.mongodb.org/browse/CSHARP-460 https://jira.mongodb.org/browse/CSHARP-460
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.