I have a base class called Entry:
[BsonDiscriminator(RootClass = true)]
[BsonKnownTypes(typeof(AccountEntry))]
public class Entry
{
public ObjectId Id { get; set; }
public string Register { get; set; }
}
With a derived class called AccountEntry as example
public class AccountEntry : Entry
{
public Account Account { get; set; }
}
When I try to do an Update, a compile time error shows that says, AccountEntry cannot be converted to 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 the derived class does not cause the same problem.
Your filter needs to be of type Entry, not AccountEntry.
This is because filter is not covariant (I think that's the right one). So, the compiler can't just use a FilterDefinitionBuilder in place of a FilterDefinitionBuilder.
We have a ticket open to better handle derived types: https://jira.mongodb.org/browse/CSHARP-1194.