Non-deterministic functions are funcletized and evaluated

XMLWordPrintableJSON

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

      Generally, non-deterministic functions (like DateTime.UtcNow) are always translated to the server, or the translation fails. This avoids using a single value (which may be cached, inlined, or parameterized) rather than calling the function on the server each time the query runs.

      For functions that can't be translated, the query should fail, rather than evaluating a single value and using it, which is what the Mongo provider does.

      For example:

      context.Set<Order>().Where(o => o.Foo > Random.Shared.Next()).ToList();
      

      Becomes:

      SomeDb3.Order.aggregate([{ "$match" : { "Foo" : { "$gt" : 1018507280 } } }]
      

      Repro:
      using var context = new SomeDbContext();
      context.Database.EnsureDeleted();
      context.Database.EnsureCreated();

      context.AddRange(
      new Order

      { Id = 1, Foo = 1 }

      ,
      new Order

      { Id = 2, Foo = 1 }

      );

      context.SaveChanges();

      var results = context.Set<Order>().Where(o => o.Foo > Random.Shared.Next()).ToList();
      foreach (var result in results)
      {
      Console.WriteLine($"

      {result.Id}

      {result.Foo}

      ");
      }

      public class SomeDbContext : DbContext
      {
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      => optionsBuilder
      //.UseSqlite("DataSource = db.dat")
      .UseMongoDB("mongodb://localhost:27017", "SomeDb3")
      .EnableSensitiveDataLogging()
      .LogTo(Console.WriteLine, LogLevel.Information);

      protected override void OnModelCreating(ModelBuilder modelBuilder)

      { modelBuilder.Entity<Order>(); }

      }

      public class Order
      {
      public int Id

      { get; set; }
      public int Foo { get; set; }

      }

      
      

            Assignee:
            Unassigned
            Reporter:
            Arthur Vickers
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: