Element 'id' does not match any field or property of error with nested classescannot deserialize with the “Id” attributeOrder of items in classes: Fields, Properties, Constructors, MethodsWhat is the difference between a field and a property?Does C# have extension properties?How to get the list of properties of a class?Element 'Id' does not match any field or property of classFloat inaccurate rounding in Mongodb with CS driverElement 'timestamp' does not match any field or property of class (Log4Mongo deserialization)Python: Handle pyMongo's ObjectId with Tornado's get_argumentMongoDB schema diagramChange type from ObjectId to string

Can I bring this power bank on board the aircraft?

Why not add cuspidal curves in the moduli space of stable curves?

Is "Ram married his daughter" ambiguous?

If I travelled back in time to invest in X company to make a fortune, roughly what is the probability that it would fail?

Job interview by video at home and privacy concerns

Bothered by watching coworkers slacking off

Does publication of the phone call ruin the basis for impeachment?

Did Joe Biden "stop a prosecution" into his son in Ukraine? And did he brag about stopping the prosecution?

Why is there such a singular place for bird watching?

How to "Start as close to the end as possible", and why to do so?

Generating numbers with cubes

Why does it seem the best way to make a living is to invest in real estate?

Manager told a colleague of mine I was getting fired soon

Does Bank Manager's discretion still exist in Mortgage Lending

Digital Bananas

Quote to show students don't have to fear making mistakes

As a team leader is it appropriate to bring in fundraiser candy?

How closely correlated is culture to geography?

How to refresh wired service getRecord manually?

What powers or limits devil promotion?

IEEE 754 square root with Newton-Raphson

Airport Security - advanced check, 4th amendment breach

The answer is a girl's name (my future granddaughter) - can anyone help?

Disable all sound permanently



Element 'id' does not match any field or property of error with nested classes


cannot deserialize with the “Id” attributeOrder of items in classes: Fields, Properties, Constructors, MethodsWhat is the difference between a field and a property?Does C# have extension properties?How to get the list of properties of a class?Element 'Id' does not match any field or property of classFloat inaccurate rounding in Mongodb with CS driverElement 'timestamp' does not match any field or property of class (Log4Mongo deserialization)Python: Handle pyMongo's ObjectId with Tornado's get_argumentMongoDB schema diagramChange type from ObjectId to string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









2















I've the following mongodb document schema;



 
"_id" : ObjectId("5c9d34ff781318afb9e8ab43"),
"name" : "Name",
"slug" : "slug",
"services" :
"subservice" :
"id" : NumberInt(37030)





and then i define my classes as;



public class MainModel

public ObjectId Id get; set;

[BsonElement("name")]
public string Name get; set;

[BsonElement("slug")]
public string Slug get; set;

[BsonElement("services")]
public ServicesDef Services get; set;

public class ServicesDef

[BsonElement("subservice")]
public SubServiceDef SubService get; set;

public class SubServiceDef

[BsonElement("id")]
public int Id get; set;





But somehow when I query the document;



var result = await Repository.FindAsync(x => x.Slug == slug);


That services.subservice.id isn't properly registered and getting



Element 'id' does not match any field or property of class SubServiceDef.



Stuck here and looking for advice.



I think I'm having the same issue with cannot deserialize with the "Id" attribute but seems there is solution yet.










share|improve this question
































    2















    I've the following mongodb document schema;



     
    "_id" : ObjectId("5c9d34ff781318afb9e8ab43"),
    "name" : "Name",
    "slug" : "slug",
    "services" :
    "subservice" :
    "id" : NumberInt(37030)





    and then i define my classes as;



    public class MainModel

    public ObjectId Id get; set;

    [BsonElement("name")]
    public string Name get; set;

    [BsonElement("slug")]
    public string Slug get; set;

    [BsonElement("services")]
    public ServicesDef Services get; set;

    public class ServicesDef

    [BsonElement("subservice")]
    public SubServiceDef SubService get; set;

    public class SubServiceDef

    [BsonElement("id")]
    public int Id get; set;





    But somehow when I query the document;



    var result = await Repository.FindAsync(x => x.Slug == slug);


    That services.subservice.id isn't properly registered and getting



    Element 'id' does not match any field or property of class SubServiceDef.



    Stuck here and looking for advice.



    I think I'm having the same issue with cannot deserialize with the "Id" attribute but seems there is solution yet.










    share|improve this question




























      2












      2








      2








      I've the following mongodb document schema;



       
      "_id" : ObjectId("5c9d34ff781318afb9e8ab43"),
      "name" : "Name",
      "slug" : "slug",
      "services" :
      "subservice" :
      "id" : NumberInt(37030)





      and then i define my classes as;



      public class MainModel

      public ObjectId Id get; set;

      [BsonElement("name")]
      public string Name get; set;

      [BsonElement("slug")]
      public string Slug get; set;

      [BsonElement("services")]
      public ServicesDef Services get; set;

      public class ServicesDef

      [BsonElement("subservice")]
      public SubServiceDef SubService get; set;

      public class SubServiceDef

      [BsonElement("id")]
      public int Id get; set;





      But somehow when I query the document;



      var result = await Repository.FindAsync(x => x.Slug == slug);


      That services.subservice.id isn't properly registered and getting



      Element 'id' does not match any field or property of class SubServiceDef.



      Stuck here and looking for advice.



      I think I'm having the same issue with cannot deserialize with the "Id" attribute but seems there is solution yet.










      share|improve this question
















      I've the following mongodb document schema;



       
      "_id" : ObjectId("5c9d34ff781318afb9e8ab43"),
      "name" : "Name",
      "slug" : "slug",
      "services" :
      "subservice" :
      "id" : NumberInt(37030)





      and then i define my classes as;



      public class MainModel

      public ObjectId Id get; set;

      [BsonElement("name")]
      public string Name get; set;

      [BsonElement("slug")]
      public string Slug get; set;

      [BsonElement("services")]
      public ServicesDef Services get; set;

      public class ServicesDef

      [BsonElement("subservice")]
      public SubServiceDef SubService get; set;

      public class SubServiceDef

      [BsonElement("id")]
      public int Id get; set;





      But somehow when I query the document;



      var result = await Repository.FindAsync(x => x.Slug == slug);


      That services.subservice.id isn't properly registered and getting



      Element 'id' does not match any field or property of class SubServiceDef.



      Stuck here and looking for advice.



      I think I'm having the same issue with cannot deserialize with the "Id" attribute but seems there is solution yet.







      c# mongodb mongodb-.net-driver






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 23:33









      mickl

      21.2k6 gold badges20 silver badges42 bronze badges




      21.2k6 gold badges20 silver badges42 bronze badges










      asked Mar 28 at 21:14









      HuseyinUsluHuseyinUslu

      2,9624 gold badges28 silver badges43 bronze badges




      2,9624 gold badges28 silver badges43 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1
















          Long story short: it's all about conventions. MongoDB .NET driver exposes static class ConventionRegistry which allows you to register your own conventions (more here). Additionally there are two "built-in" conventions __defaults__ and __attributes__. Digging deeper (driver github) you can find that it registers one quite interesting convention:



          new NamedIdMemberConvention(new [] "Id", "id", "_id" )


          Which means that id members will be considered as regular BSON _id elements.



          How to fix that ?



          You can get rid of default conventions



          ConventionRegistry.Remove("__defaults__");


          However automatically you will drop all the other driver conventions which is pretty risky. Alternatively you can create a fake property which will always be empty:



          public class SubServiceDef

          [BsonElement("id")]
          public int Id get; set;

          [BsonId]
          public ObjectId FakeId get; set;



          or you can just use BsonNoId attribute which




          Specifies that the class's IdMember should be null.




          [BsonNoId]
          public class SubServiceDef

          [BsonElement("id")]
          public int Id get; set;



          So the convention will be setting your id as IdMember in class map but then during postprocessing this attribute will force IdMember to be null and your class will get deserialized succesfully






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406944%2felement-id-does-not-match-any-field-or-property-of-error-with-nested-classes%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1
















            Long story short: it's all about conventions. MongoDB .NET driver exposes static class ConventionRegistry which allows you to register your own conventions (more here). Additionally there are two "built-in" conventions __defaults__ and __attributes__. Digging deeper (driver github) you can find that it registers one quite interesting convention:



            new NamedIdMemberConvention(new [] "Id", "id", "_id" )


            Which means that id members will be considered as regular BSON _id elements.



            How to fix that ?



            You can get rid of default conventions



            ConventionRegistry.Remove("__defaults__");


            However automatically you will drop all the other driver conventions which is pretty risky. Alternatively you can create a fake property which will always be empty:



            public class SubServiceDef

            [BsonElement("id")]
            public int Id get; set;

            [BsonId]
            public ObjectId FakeId get; set;



            or you can just use BsonNoId attribute which




            Specifies that the class's IdMember should be null.




            [BsonNoId]
            public class SubServiceDef

            [BsonElement("id")]
            public int Id get; set;



            So the convention will be setting your id as IdMember in class map but then during postprocessing this attribute will force IdMember to be null and your class will get deserialized succesfully






            share|improve this answer





























              1
















              Long story short: it's all about conventions. MongoDB .NET driver exposes static class ConventionRegistry which allows you to register your own conventions (more here). Additionally there are two "built-in" conventions __defaults__ and __attributes__. Digging deeper (driver github) you can find that it registers one quite interesting convention:



              new NamedIdMemberConvention(new [] "Id", "id", "_id" )


              Which means that id members will be considered as regular BSON _id elements.



              How to fix that ?



              You can get rid of default conventions



              ConventionRegistry.Remove("__defaults__");


              However automatically you will drop all the other driver conventions which is pretty risky. Alternatively you can create a fake property which will always be empty:



              public class SubServiceDef

              [BsonElement("id")]
              public int Id get; set;

              [BsonId]
              public ObjectId FakeId get; set;



              or you can just use BsonNoId attribute which




              Specifies that the class's IdMember should be null.




              [BsonNoId]
              public class SubServiceDef

              [BsonElement("id")]
              public int Id get; set;



              So the convention will be setting your id as IdMember in class map but then during postprocessing this attribute will force IdMember to be null and your class will get deserialized succesfully






              share|improve this answer



























                1














                1










                1









                Long story short: it's all about conventions. MongoDB .NET driver exposes static class ConventionRegistry which allows you to register your own conventions (more here). Additionally there are two "built-in" conventions __defaults__ and __attributes__. Digging deeper (driver github) you can find that it registers one quite interesting convention:



                new NamedIdMemberConvention(new [] "Id", "id", "_id" )


                Which means that id members will be considered as regular BSON _id elements.



                How to fix that ?



                You can get rid of default conventions



                ConventionRegistry.Remove("__defaults__");


                However automatically you will drop all the other driver conventions which is pretty risky. Alternatively you can create a fake property which will always be empty:



                public class SubServiceDef

                [BsonElement("id")]
                public int Id get; set;

                [BsonId]
                public ObjectId FakeId get; set;



                or you can just use BsonNoId attribute which




                Specifies that the class's IdMember should be null.




                [BsonNoId]
                public class SubServiceDef

                [BsonElement("id")]
                public int Id get; set;



                So the convention will be setting your id as IdMember in class map but then during postprocessing this attribute will force IdMember to be null and your class will get deserialized succesfully






                share|improve this answer













                Long story short: it's all about conventions. MongoDB .NET driver exposes static class ConventionRegistry which allows you to register your own conventions (more here). Additionally there are two "built-in" conventions __defaults__ and __attributes__. Digging deeper (driver github) you can find that it registers one quite interesting convention:



                new NamedIdMemberConvention(new [] "Id", "id", "_id" )


                Which means that id members will be considered as regular BSON _id elements.



                How to fix that ?



                You can get rid of default conventions



                ConventionRegistry.Remove("__defaults__");


                However automatically you will drop all the other driver conventions which is pretty risky. Alternatively you can create a fake property which will always be empty:



                public class SubServiceDef

                [BsonElement("id")]
                public int Id get; set;

                [BsonId]
                public ObjectId FakeId get; set;



                or you can just use BsonNoId attribute which




                Specifies that the class's IdMember should be null.




                [BsonNoId]
                public class SubServiceDef

                [BsonElement("id")]
                public int Id get; set;



                So the convention will be setting your id as IdMember in class map but then during postprocessing this attribute will force IdMember to be null and your class will get deserialized succesfully







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 23:28









                micklmickl

                21.2k6 gold badges20 silver badges42 bronze badges




                21.2k6 gold badges20 silver badges42 bronze badges

































                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406944%2felement-id-does-not-match-any-field-or-property-of-error-with-nested-classes%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript