【问题标题】:EF Core 3 evaluate navigation property null on serverEF Core 3 在服务器上评估导航属性 null
【发布时间】:2020-01-03 11:03:18
【问题描述】:

我有一个要求

DbContext.Invoices
.Where(x => x.Status != InvoiceStatus.Draft && x.PaymentMethod == PaymentMethod.DirectDebit)
.Where(x => x.DirectDebitFile == null).ToList();

DirectDebitFile 是反向导航属性。

在 EF Core 2 中运行良好,不确定在最终请求中是如何评估的。 升级到 EF Core 3 后,此请求不再起作用并显示

System.InvalidOperationException: The LINQ expression 'DbSet<Invoice>
    .Where(i => !(i.IsDeleted))
    .Where(i => i.ClubId == __CurrentUser_ClubId_0)
    .Cast()
    .Where(e => e.FederationId == __CurrentUser_FederationId_1)
    .Cast()
    .Where(e0 => !(e0.Hidden))
    .Cast()
    .Where(e1 => (int)e1.Status != 0 && (Nullable<int>)e1.PaymentMethod == (Nullable<int>)DirectDebit)
    .LeftJoin(
        outer: DbSet<DirectDebitFile>
            .Where(d => !(d.IsDeleted)), 
        inner: e1 => EF.Property<Nullable<long>>(e1, "Id"), 
        outerKeySelector: d => EF.Property<Nullable<long>>(d, "InvoiceId"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<Invoice, DirectDebitFile>(
            Outer = o, 
            Inner = i
        ))
    .Where(e1 => e1.Inner == 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

我可以重写此查询并通过将评估移至客户端使其工作

DbContext.Invoices
.Include(x=>x.DirectDebitFile)
.Where(x => x.Status != InvoiceStatus.Draft && x.PaymentMethod == PaymentMethod.DirectDebit)
.AsEnumerable()
.Where(x => x.DirectDebitFile == null).ToList();

但在这种情况下,当然,查询会提取所有行,并且过滤 x.DirectDebitFile == null 将在客户端进行。我希望在服务器上评估此查询,请帮助实现。

【问题讨论】:

    标签: ef-core-3.0 ef-core-3.1


    【解决方案1】:

    目前,我通过检查连接表字段之一将请求更改为普通 SQL 检查它的方式

    AppDbContext.Invoices.Include(x => x.DirectDebitFile)
                    .Where(x => x.Status != InvoiceStatus.Draft)
                    .Where(x => x.DirectDebitFile.FileName == null);
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 2018-05-09
      • 2018-10-22
      • 2018-10-10
      • 2020-07-04
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多