MongoDB бросает исключение, когда у модала есть динамическое полеC#

Место общения программистов C#
Anonymous
 MongoDB бросает исключение, когда у модала есть динамическое поле

Сообщение Anonymous »

Я пытаюсь перенести свою базу данных из космоса в Mongo.
При попытке добавить запись для объекта, который имеет динамический тип C# в исключении MongoDB, из -за API insertoneasync (), предоставляемого mongodb.driver.imongocollection
dynamic type, используемый для работы в Cosmos db. Код:

Код: Выделить всё

_collection.InsertOneAsync(document);

Модель:

Код: Выделить всё

namespace Entities.Models
{
[BsonIgnoreExtraElements]
public class UPModel
{
[JsonProperty(PropertyName = "_id")]
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string BsonId { get; set; } = string.Empty;

[BsonElement("resource")]
public Resource Resource{ get; set; }

[BsonElement("preference")]
public Preference Preference { get; set; }
}

[BsonIgnoreExtraElements]
public class Resource
{
[BsonElement("id")]
public string Id { get; set; }

[BsonElement("identifiers")]
public dynamic Identifiers { get; set; }
}

[BsonIgnoreExtraElements]
public class Preference
{
[BsonElement("id")]
public string Id { get; set; }

[BsonElement("value")]
public dynamic Value { get; set; }
}
}
< /code>
Документ, который необходимо создать для этой модели в DB: < /p>
{
"id":"uniqueIdentifier",
"preference": {
"id": "dummyPreferenceId",
"value": {
"favorites": [
"dummyFavorites1"
]
}
},
"resource": {
"identifiers": {
"userId": 99,
"routeUrl": "dummyRouteURL",
"type": "standard"
},
"id": "dummyResourceId"
}
}
< /code>
Исключение: < /p>
MongoDB.Bson.BsonSerializationException
HResult=0x80131500
Message=An error occurred while serializing the Resource property of class UPModel: An error occurred while serializing the Identifiers property of class Resource: Type Newtonsoft.Json.Linq.JObject is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer.
Source=MongoDB.Bson
StackTrace: "Removed for Readability"
Inner Exception 1:
BsonSerializationException: An error occurred while serializing the Resource property of class UPModel: An error occurred while serializing the Identifiers property of class Resource: Type Newtonsoft.Json.Linq.JObject is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer.

Inner Exception 2:
BsonSerializationException: An error occurred while serializing the Identifiers property of class Resource: Type Newtonsoft.Json.Linq.JObject is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer.

Inner Exception 3:
BsonSerializationException: Type Newtonsoft.Json.Linq.JObject is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer.
Я ищу способ сохранить динамический тип объекта C# в mongodb

Подробнее здесь: https://stackoverflow.com/questions/794 ... amic-field

Вернуться в «C#»