If you follow a bit the progress of the .NET team, you probably know that Entity Framework Core 3.0 will be released in the upcoming weeks bringing a lot of improvements. This article is not about these ones as there are already a lot of articles about that (for example this one). Instead, I’d like to highlight a very important change and how to prepare yourself for it.
If you have a look at the breaking changes part of the Microsoft docs article, you’ll read that one of the biggest breaking change is the fact that queries won’t be evaluated on the client side anymore. In other words, if you try to execute a query that cannot be fully translated in SQL, an exception will be thrown. At first sight, I thought that it was really crazy to introduce such a major change but when you think about it, it makes sense.
Imagine that you have a table with 1 000 000 records in it and you want to retrieve an item using a LINQ query that cannot be fully translated. Say that there are normally only 5 items that match your criteria but as your query cannot be fully translated, EF Core will actually load the whole table (yes yes, the 1 000 000 records) in memory and evaluate your query in memory. I’m sure I don’t have to explain why it’s bad… Now, in EF Core 3.0, this kind of scenario won’t be possible anymore as an exception would be thrown.
However, I might seem difficult to know how to prepare yourself for this breaking changes. Well… By searching over the Internet about tips to improve EF Core usage, I found out that you can already configure EF Core 2.0 to throw an exception if a query needs client side evaluation that way:
Now an exception will be thrown as soon as query cannot be fully translated in SQL.