EF Core 3评估服务器上的导航属性为空

时间:2020-01-03 11:03:18

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

我有一个请求

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将在客户端。我希望在服务器上评估该查询,请帮助实现该查询。

1 个答案:

答案 0 :(得分:0)

当前,我通过检查联接表字段之一来更改请求,就像通常的SQL检查请求的方式一样

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