MongoDB's C # official driver InvalidOperationException Exception Solution

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.