Cross-document navigation comparisons do not translate

XMLWordPrintableJSON

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

      For example:

      var results = from animal in context.Set<Animal>()
          where animal.Details != null
          select animal;
      

      Throws:

      Unhandled exception. System.InvalidOperationException: The LINQ expression 'DbSet<Animal>()
          .Where(animal => animal.Details != null)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
         at MongoDB.EntityFrameworkCore.Query.MongoQueryCompilationContext.CreateQueryExecutor[TResult](Expression originalQuery)
         at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass11_0`1.<ExecuteCore>b__0()
         at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
         at Program.<Main>$(String[] args) in /Users/arthur.vickers/code/OnYourOwnNow/OnYourOwnNow/Program.cs:line 29
      

      Closely related to EF-117 (Include) and EF-202 (Entity equality)

      Full repro

      using var context = new SomeDbContext();
      
      context.Database.EnsureDeleted();
      
      context.Add(
              new Animal { Id = 1 });
      
      context.Add(
              new AnimalDetails
              {
                  Id = 1,
                  AnimalId = 1,
                  BoolField = true
              });
      
      context.SaveChanges();
      
      var results = from animal in context.Set<Animal>()
          where animal.Details != null
          select animal;
      
      foreach (var result in results)
      {
          Console.WriteLine($"Found {result}");
      }
      
      
      public class SomeDbContext : DbContext
      {
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
              => optionsBuilder
                  .UseMongoDB("mongodb://localhost:27017", "SomeDB")
                  .LogTo(Console.WriteLine, LogLevel.Information);
      
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity<Animal>();
              modelBuilder.Entity<AnimalDetails>();
          }
      }
      
      
      public class Animal
      {
          public int Id { get; set; }
          public AnimalDetails? Details { get; set; }
      }
      
      public class AnimalDetails
      {
          public int Id { get; set; }
          public int? AnimalId { get; set; }
      
          public bool BoolField { get; set; }
      }
      

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

                Created:
                Updated: