mongodb - How to upsert a derived class in C# -


i have base class called entry:

[bsondiscriminator(rootclass = true)] [bsonknowntypes(typeof(accountentry))] public class entry {     public objectid id { get; set; }     public string register { get; set; } } 

with derived class called accountentry example

public class accountentry : entry {     public account account { get; set; } } 

when try update, compile time error shows says, accountentry cannot converted entry

var filter = builders<accountentry>.filter.where(x => x.id = id);             await context.entries.updateoneasync(filter, new accountentry() {                                                     account = debitaccount,                 }, new updateoptions() { isupsert = true }); 

inserting derived class not cause same problem.

your filter needs of type entry, not accountentry.

var filter = builders<entry>.filter.where(x => x.id == id); 

this because filter not covariant (i think that's right one). so, compiler can't use filterdefinitionbuilder in place of filterdefinitionbuilder.

we have ticket open better handle derived types: https://jira.mongodb.org/browse/csharp-1194.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -