Uploaded image for project: 'Entity Framework'
  1. Entity Framework
  2. EF-101

Null embedded document data causes InvalidCastException even if [BsonIgnoreIfNull] is used

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.0
    • Affects Version/s: Public Preview 1
    • None
    • Dotnet Drivers

      Simple data example with parent document and enumerable child document:

      {{public class TestData {
      public Guid Id

      { get; set; }

      [BsonIgnoreIfNull]
      public IEnumerable<TestChildData>? ChildData { get; set; }


      }

      public class TestChildData {
      public Guid Id

      { get; set; }

      }

      protected override void OnModelCreating(ModelBuilder modelBuilder)

      { base.OnModelCreating(modelBuilder); modelBuilder .Entity<TestData>() .ToCollection(_config.Value.CollectionName) .OwnsMany(x => x.ChildData); }

      }}

      If ChildData is null in the database, simply trying to ToList() the {{DbSet }}results in an exception:

      var data = _dbContext?.TestData.ToList();

      System.InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonNull' to type 'MongoDB.Bson.BsonArray'.
      at lambda_method17(Closure, QueryContext, BsonDocument)
      at MongoDB.EntityFrameworkCore.Query.QueryingEnumerable`2.Enumerator.MoveNextHelper()
      at MongoDB.EntityFrameworkCore.Query.QueryingEnumerable`2.Enumerator.MoveNext()
      at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
      at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

      Likely related to EF-99 and EF-63. This is very basic functionality that should be supported without so many problems.

            Assignee:
            damien.guard@mongodb.com Damien Guard
            Reporter:
            tonyhernandez743@gmail.com Anthony Hernandez
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: