Description of the exception
There is an "article" class, which contains a "list< paragraph >" Type of attribute, the "paragraph" class is an abstract class, its subclasses have "subtitle paragraph", "text paragraph", "image paragraph", "attachment paragraph", "list paragraph" and other types.
It is no problem to store an "article" type of object in a MongoDB database, but retrieving it from the database throws an exception like this:
[InvalidOperationException: an instance of an abstract class could not be created. ]
Workaround
Previously consulted with Magicdict on this issue, his approach was to refer to this post: http://www.cnblogs.com/zlp520/p/3921435.html
I didn't do the actual measurement because I found a seemingly simpler approach:
1. In the paragraph class header labeling characteristics:
[Bsonknowntypes (typeoftypeoftypeoftypeoftypeof(image paragraph)])
2. Declare before instantiating the mongoclient:
Bsonclassmap.lookupclassmap (typeof(mongomodels. paragraph));
This is a perfect solution to this problem.
Summarize
This workaround applies to cases in which the default type in a collection is abstract type, except in cases where the attribute of the abstract type is included.
The interface is not tested, and the same error is reported in the impression, as is the case with the Guess solution.
Note that this method is only suitable for situations where you can modify the source code, and the resolution of the article recommended by Magicdict is also available if the source code cannot be modified.
MongoDB's C # official driver InvalidOperationException Exception Solution