c# - c# 如何将 Array 对象属性映射到 Mongodb BsonType.Array
问题描述
我的模型中有以下对象:
[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 数组?
解决方案
如果我只是这样制作属性:
public List<string> SomeList = new List<string>();
它会映射,不需要表示。
推荐阅读
- javascript - 使用 ibm watson 在聊天机器人中发送获取请求时如何解决错误?
- javascript - 如何使用 Apps-Script 在 Google 电子表格列中标记/突出显示多种货币
- django - 在 Django rest 框架中发出 put 请求时出现逻辑错误
- reactjs - React history.push() 正在更新 url 但不在浏览器中导航到它?
- node.js - _.omit 在 monogdb 对象上使用时不返回预期值
- solidity - 有人用过 The Graph 吗?我在创建子图时遇到问题
- android - 在 Android 上执行周期性任务的最可靠方法
- java - TemplateInputException:模板解析期间发生错误(模板:“类路径资源[templates/index.html]”)
- task - DAG 中的任务在已运行时尝试运行
- css - 如何在图像上使用边框半径制作插入边框