簡體   English   中英

帶有布爾異步調用的 EF Core 3.1 客戶端評估問題

[英]EF Core 3.1 Client Side Evaluation Issue With Boolean Async Call

我正在從 .Net 2.1 遷移到 3.1,這包括 EF Core 升級。

現在我有如下 LINQ 查詢,沒有任何問題:

var application = await _db.CustomerApplications
                .AsNoTracking()
                .Include(i => i.CustomerApplicationFields)
                .Include(i => i.Customer)
                .Where(x => x.Customer.PublicId == formId && x.IsPublished) // Also tried with &
                .OrderByDescending(o => o.Version)
                .FirstOrDefaultAsync();

使用 EF Core 3.1 時出現錯誤:

The LINQ expression 'DbSet<CustomerApplication>
    .Where(c => !(c.Deleted))
    .Join(
        outer: DbSet<Customer>
            .Where(c0 => !(c0.Deleted)), 
        inner: c => EF.Property<Nullable<long>>(c, "CustomerId"), 
        outerKeySelector: c0 => EF.Property<Nullable<long>>(c0, "Id"), 
        innerKeySelector: (o, i) => new TransparentIdentifier<CustomerApplication, Customer>(
            Outer = o, 
            Inner = i
        ))
    .Where(c => c.Inner.PublicId == __formId_0 && c.Outer.IsPublished)' 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.

當我按如下方式轉換此查詢時,它會起作用(將bool評估移到外面):

var application = await _db.CustomerApplications
                .AsNoTracking()
                .Include(i => i.CustomerApplicationFields)
                .Include(i => i.Customer)
                .Where(x => x.Customer.PublicId == formId)
                .OrderByDescending(o => o.Version)
                .ToListAsync();

var result = application.FirstOrDefault(x => x.IsPublished);

有人可以向我解釋一下,為什么這是一個問題? 我也試過x.IsPublished == true ,但沒有效果。 這似乎是非常隨機的。

我也試過AsTracking()

在 EF Core 3.0 之前,無法轉換為 SQL 查詢的查詢在客戶端進行評估。 現在,此行為已被取消,並拋出異常,而不是在客戶端評估不可翻譯的查詢。

此外,我認為當您編寫var result = application.FirstOrDefault(x => x.IsPublished);時,新行為不應導致任何大的性能問題 分開,因為同樣的事情以前發生過。 只是以前是看不到的。 (如果這個假設是錯誤的,請糾正我!)

如果您想進行一個查詢(未對此進行測試),您也可以嘗試以下操作:

var application = await _db.CustomerApplications
            .AsNoTracking()
            .Include(i => i.CustomerApplicationFields)
            .Include(i => i.Customer)
            .Where(x => x.Customer.PublicId == formId)
            .OrderByDescending(o => o.Version)
            .FirstOrDefaultAsync(x => x.IsPublished);

您可以在此處詳細閱讀: https : //docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/break-changes#linq-queries-are-no - 客戶評估時間更長

您應該使用 && 而不是 & 或者您可以添加另一個 where 子句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM