3
votes

I have this Bson model in mongodb:

"setting|resources" : {
                    "Name" : "EsupHamrah",
                    "Id" : "449ea0e1-0261-4bee-b096-a838746c94ea",
                    "Children" : [..

I have created this contract model :

public class SettingResources
    {
        public string Name { get; set; }

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

        //[BsonId(IdGenerator = typeof(CombGuidGenerator))]
        //public string Id { get; set; }

        public IList<object> Children { get; set; }
    }

When i do query I got tthis error:

An error occurred while deserializing the configs property of class ConsoleMongoApp.Applications: An error occurred while deserializing the values property of class ConsoleMongoApp.Configs: An error occurred while deserializing the resources property of class ConsoleMongoApp.Values: Element 'Id' does not match any field or property of class ConsoleMongoApp.SettingResources.

The Id is just string, not GUID or ObjectId but why mongo can not do map the id?

1
@user9405863 no, i've just commented [BsonRepresentation(BsonType.String)].With this i still got above error.Cyrus the Great
did you try removing [BsonElement] annotation?user9405863
@user9405863 yes but nothing is change and i still have error on id.Seems mongo has a problem on Id element.Cyrus the Great
add [BsonIgnoreExtraElements] to class SettingResources and i think you would be gooduser9405863
@user9405863 ,No I need this element.I added [BsonIgnoreExtraElements] for testing and error was gone but i got id = null;Cyrus the Great

1 Answers

1
votes

your model in mongodb is wrong, there is a | (pipe) in Json string, try fix it on "settingResources":

"settingResources" : {
                    "Name" : "EsupHamrah",
                    "Id" : "449ea0e1-0261-4bee-b096-a838746c94ea",
                    "Children" : [..