[CSHARP-2380] Projection doesn't work after OfType Created: 07/Sep/18  Updated: 16/Nov/21  Resolved: 14/Dec/18

Status: Closed
Project: C# Driver
Component/s: Read Operations
Affects Version/s: 2.7.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Max Drachsh Assignee: Vincent Kam (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, x64.



 Description   

I use polymorphism and have the following objects:

 

public abstract class Entity : IEntity
{
 [BsonId]
 [BsonIgnoreIfDefault]
 [BsonRepresentation(BsonType.ObjectId)]
 public string Id { get; set; }
 [BsonIgnore]
 public abstract string CollectionName { get; }
}
[BsonDiscriminator(Required = true, RootClass = true)]
[BsonKnownTypes(typeof(LifeAccount), typeof(SimAccount))]
public class Account2 : Entity
{
 public string Name { get; set; }
 public string UserId { get; set; }
 public DateTime CreatedAt { get; set; }
 public DateTime? LastLoginAt { get; set; }
 public override string CollectionName { get; } = "Accounts";
}
public class LifeAccount : Account2
{
 public string ConsumerKey { get; set; }
 public string ConsumerSecret { get; set; }
}
 
public class SimAccount : Account2
{
 public decimal TransactionFee { get; set; }
 public decimal NetAssets { get; set; }
}

 

 

Then, I make query with `OfType` and `Project`:

 

private readonly IMongoCollection<Account2> _accountCollection;
...
var projectDef = Builders<LifeAccount>.Projection
 .Include(a => a.Name)
 .Include(a => a.ConsumerKey)
 .Include(a => a.ConsumerSecret);
 
var account = await _accountCollection.OfType<LifeAccount>().Find(a => a.Id == accountId)
 .Project<LifeAccount>(projectDef)
 .SingleOrDefaultAsync();

 

 

I get error: 

"Element 'ConsumerKey' does not match any field or property of class Trader.Domain.Account2."

 

If I delete `Project` then all works fine.

 



 Comments   
Comment by Vincent Kam (Inactive) [ 14/Nov/18 ]

Hello Max,
Thank you for submitting the bug report! I had some trouble reproducing the bug: would you be able to share a small sample project with a reproduction so that I can look into the issue you're experiencing?

Kind regards,
Vincent

Generated at Wed Feb 07 21:42:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.