MongoDB löst eine Ausnahme aus, wenn das Modal ein dynamisches Feld hat
Posted: 27 Feb 2025, 11:32
Ich versuche, meine Datenbank von Kosmos nach Mongo zu migrieren. />
Modell:
Ich suche nach einer Möglichkeit, den dynamischen Objekttyp C# in MongoDB
Code: Select all
_collection.InsertOneAsync(document);
Modell:
Code: Select all
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>
Dokument, das für dieses Modell im DB erstellt werden muss: < /p>
{
"id":"uniqueIdentifier",
"preference": {
"id": "dummyPreferenceId",
"value": {
"favorites": [
"dummyFavorites1"
]
}
},
"resource": {
"identifiers": {
"userId": 99,
"routeUrl": "dummyRouteURL",
"type": "standard"
},
"id": "dummyResourceId"
}
}
< /code>
Ausnahme: < /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.