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

mongodb ef 8.2.0 error - The source argument must be a MongoDB IQueryable

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Dotnet Drivers

      mongodb.efcore version : 8.2.0
      mongodb.driver version : 3.0.0
      mongodb.bson verion : 3.0.0

      //test code
      var client = new MongoClient(_host);
      var db = client.GetDatabase("healthcare-db");
      var dbContext = MongoDbContext.Create(db);
      var item = new SampleObject()
      {
          Name = "test",
          Age = 10,
          Weight = 100
      };
      await dbContext.Samples.AddAsync(item);
      await dbContext.SaveChangesAsync();
      
      // here error "The source argument must be a MongoDB IQueryable"
      var items = await dbContext.Samples.AsQueryable().Where(m => m.Name == item.Name).ToListAsync();
      
      //...
      
      //dbcontext
      public class MongoDbContext : DbContext
      {
          /// <summary>
          /// ctor
          /// </summary>
          /// <param name="options"></param>
          public MongoDbContext(DbContextOptions options)
              : base(options)
          {
          }
          
          public static MongoDbContext Create(IMongoDatabase database) =>
              new(new DbContextOptionsBuilder<MongoDbContext>()
                  .UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
                  .Options);
      
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
      #if DEBUG
              optionsBuilder.EnableSensitiveDataLogging()
                  .EnableThreadSafetyChecks()
                  .EnableDetailedErrors();
      #endif
              
              base.OnConfiguring(optionsBuilder);
          }
      
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {   
              base.OnModelCreating(modelBuilder);
              modelBuilder.Entity<SampleObject>().ToCollection("samples");
          }
      
          public DbSet<SampleObject> Samples { get; set; }
      }
      
      
      public class SampleObject
      {
          [BsonId] 
          [BsonElement("_id")]
          public ObjectId _id { get; set; }
          
          [BsonElement("name")]
          public string Name { get; set; }
          
          [BsonElement("age")]
          public int Age { get; set; }
          
          [BsonElement("weight")]
          public double Weight { get; set; }
      }

      error message :

      System.ArgumentException: The source argument must be a MongoDB IQueryable. (Parameter 'source')
         at MongoDB.Driver.Linq.MongoQueryable.GetCursorSource[TDocument](IQueryable`1 source)
         at MongoDB.Driver.Linq.MongoQueryable.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
         at Wellness.Test.UtilTestCase.MongoTest.mongodb_sample_test1() in D:\workspace\HiCardi_WLS_Api\src\Wellness.Test\UtilTestCase\MongoTest.cs:line 199

       

            Assignee:
            damien.guard@mongodb.com Damien Guard
            Reporter:
            h20913@gmail.com SEOKWON HONG
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: