c# - Get documents for base class and inheriting classes in one query -
c# - Get documents for base class and inheriting classes in one query -
i have mongo collection
of taskbase
documents. taskbase
has 3 subclasses. created collection manager collection (from generic manager use). when create, update or retrieve subclass of taskbase
right type , no exception.
i created next method:
public ienumerable<taskbase> gettasksbyappid(string appid) { var entityquery = query<taskbase>.eq(t => t.appoid, appid); homecoming this.mongoconnectionhandler.mongocollection.find(entityquery).tolist(); }
when run exception element [some element existing in subclass] not property or fellow member of taskbase
understand why getting exception, don't know it. collection of types of tasks associated app.
you need show driver class hierarchy. there 2 options, first using bsonknowntypes
, bsondiscriminator
attributes, other using bsonclassmap
.
decorate base of operations class specific derived classes want include (similar in wcf
). tell driver it's root need bsondiscriminator
:
[bsondiscriminator(rootclass = true)] [bsonknowntypes(typeof(concretetask))] public class taskbase { }
bsonclassmap bsonclassmap.registerclassmap<taskbase>(); bsonclassmap.registerclassmap<concretetask>();
result as result, document's type discriminator (_t) array, , not single value. in case:
{ _t : ["taskbase", "concretetask"] }
c# .net mongodb polymorphism mongodb-csharp
Comments
Post a Comment