Search code examples
c#mongodbmongodb-.net-driver

Convention not applied unless first db query does not reference serialized object members


Using the mongodb C# driver, when I add a convention in a convention pack to the convention registry, the convention does not work correctly if my first database query references a member of a serialize object affected by the convention.

For example, if my first query after register my Camel Case name convention is something like myCollection.FindAll().ToList(), its all good.

But if I have a query that filters on an object property and then I try to use it like myCollection.Find(myQuery).ToList() it doesn't work (the convention is not mapped properly causing an error). Further it doesn't work for that collection object from that point forward.

Is this supposed to work like this? If so does this mean I need to make a "dummy" query to the db after I register my convention so that it honors my registered conventions?


Solution

  • I reread the article on how to apply conventions and as pointed out in the comments, this should really be registered in app startup. I did this and the convention behaved as expected. I still don't fully understand the details of why it was acting the way it did, but I'm satisfied with: if I wire it up correctly, it works correctly.