Open Almis90 opened 6 years ago
This is a fairly old issue, but I ran into it and looked into it a bit.
This is caused by recursive/mutually recursive references. BsonMapper.RegisterDbRefItem
and RegisterDbRefList
rely on getting the entity mapper for the referenced type. This is done when the field is registered so that we can avoid looking up the entity mapper each time the field is de/serialized. Unfortunately, LiteDb attempts to build a new mapper whenever it encounters a type that isn't mapped yet.
Also it's worth noting this is still an issue in v5.
The easiest workaround is to just store the ID manually and resolve the reference yourself on an as-needed basis.
If you're willing to modify LiteDB (or make your own BsonMapper
) and don't mind the performance hit, I very briefly tested moving the entity mapper lookup into the de/serialization methods for reference fields. (Diff here)
This works, but it is not particularly efficient. If you really want this, I think a better strategy would be to cache the mapper on the first de/serialization.
I'm having the same problem, and the answer above is right, it does happen when referencing with BsonRef mutually two classes and one is not mapped yet, @mbdavid @lbnascimento
My models looks like this
I know this is wrong because I getting the below exception but what is the right way to do that?