efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Client-Side Evaluation Behavior Difference Between EF 6 and EF Core 8

Open RezaRahmatzadeh opened this issue 1 year ago • 4 comments

We recently migrated our .NET Framework application to .NET 8 and encountered multiple exceptions related to complex queries in Entity Framework Core. Specifically, the error indicates that EF Core cannot translate the query into SQL, suggesting that we perform client-side evaluation using methods like AsEnumerable() or ToList().

My question is:

Was Entity Framework 6 silently performing client-side evaluation in such cases without raising an error? Is the new behavior in EF Core designed to raise exceptions intentionally to alert developers about potential inefficiencies?

Thank you for your clarification!

RezaRahmatzadeh avatar Dec 20 '24 23:12 RezaRahmatzadeh

A concrete example would be helpful here...

ErikEJ avatar Dec 21 '24 05:12 ErikEJ

A concrete example would be helpful here...

There are several examples of this issue. One of the most common is queries that include an aggregation in subqueries. While such queries work correctly in Entity Framework 6, they raise exceptions in EF Core because SQL Server cannot process them as written.

For reference, this issue is documented here: https://github.com/dotnet/efcore/issues/34256

I'm curious why EF 6 handles these queries without errors. Does it silently switch to client-side evaluation (in C# code rather than SQL)?

RezaRahmatzadeh avatar Dec 21 '24 05:12 RezaRahmatzadeh

@RezaRahmatzadeh as far as I know, non-core EF6 did not use client evaluation in this way. However, it sometimes went very far in terms of translating absolutely every possible LINQ construct, generating SQL that was in some cases quite inefficient. The EF Core approach is different - where a query can't be translated efficiently, we sometimes prefer to fail the translation, guiding the user to rewrite their LINQ query in a more efficient way, rather than have "everything work" and silently cover up problematic performance.

In addition to the above, there are also still certain queries which EF Core could translate, but doesn't (yet), simply because we haven't gotten around to implementing those translations, and user demand for them has been low.

In any case, if you're interested in understanding what's going on (cilent evaluation or not), the easiest thing is to simply examine the SQL produce by non-core EF6.

roji avatar Dec 21 '24 08:12 roji

@RezaRahmatzadeh as far as I know, non-core EF6 did not use client evaluation in this way. However, it sometimes went very far in terms of translating absolutely every possible LINQ construct, generating SQL that was in some cases quite inefficient. The EF Core approach is different - where a query can't be translated efficiently, we sometimes prefer to fail the translation, guiding the user to rewrite their LINQ query in a more efficient way, rather than have "everything work" and silently cover up problematic performance.

In addition to the above, there are also still certain queries which EF Core could translate, but doesn't (yet), simply because we haven't gotten around to implementing those translations, and user demand for them has been low.

In any case, if you're interested in understanding what's going on (cilent evaluation or not), the easiest thing is to simply examine the SQL produce by non-core EF6.

Got it, thanks!

RezaRahmatzadeh avatar Dec 21 '24 16:12 RezaRahmatzadeh