【问题标题】:c# How can I map an Array object property to a Mongodb BsonType.Arrayc# 如何将 Array 对象属性映射到 Mongodb BsonType.Array
【发布时间】:2019-11-10 16:49:23
【问题描述】:

我的模型中有以下对象:

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

[BsonElement("Name")]
[BsonRepresentation(BsonType.String)]
public string Name { get; set; }

[BsonElement("Price")]
[BsonRepresentation(BsonType.Double)]
public double Price { get; set; }

[BsonElement("Type")]
[BsonRepresentation(BsonType.Array)]
public string[] Type { get; set; }

当我尝试使用 => 将新对象插入数据库时​​

MongoCollection.InsertOne(object);

出现以下错误

System.TypeInitializationException: The type initializer for 'OrdisTT.Libs.MongoLib' threw an exception.
 ---> System.ArgumentException: GenericArguments[0], 'System.Array', on 'MongoDB.Bson.Serialization.Serializers.EnumerableInterfaceImplementerSerializer`1[TValue]' violates the constraint of type 'TValue'.
 ---> System.TypeLoadException: GenericArguments[0], 'System.Array', on 'MongoDB.Bson.Serialization.Serializers.EnumerableInterfaceImplementerSerializer`1[TValue]' violates the constraint of type parameter 'TValue'.
   at System.RuntimeTypeHandle.Instantiate(Type[] inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---

如何将数组属性映射到 mongodb 数组?

【问题讨论】:

标签: c# arrays database mongodb bson


【解决方案1】:

如果我只是这样设置属性:

public List<string> SomeList = new List<string>();

它会映射,不需要表示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 2020-02-10
    • 2012-01-23
    • 2022-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多