Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2380

Projection doesn't work after OfType

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.7.0
    • Component/s: Read Operations
    • None
    • Environment:
      Windows 10, x64.

      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.

       

            Assignee:
            vincent.kam@mongodb.com Vincent Kam (Inactive)
            Reporter:
            sharok89@gmail.com Max Drachsh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: