EF Linq Error after change from dotnet Core 2.2.6 to 3.0.0
The reason is that implicit client evaluation has been disabled in EF Core 3.
What that means is that previously, your code didn't execute the WHERE clause on the server. Instead, EF loaded all rows into memory and evaluated the expression in memory.
To fix this issue after the upgrade, first, you need to figure out what exactly EF can't translate to SQL. My guess would be the call to GetValueOrDefault()
, therefore try rewriting it like this:
.Where(x => x.BirthDate != null && x.BirthDate.Value.Month == DateTime.Now.Month)
Entity Framework 3.0 Contains cannot be translated in SQL as it was in EF Core 2.2
It's a 3.0 bug, tracked by #17342: Contains on generic IList/HashSet/ImmutableHashSet will throw exception.
Already fixed in 3.1. The workaround (if you can't wait) is to force the usage of Enumerable.Contains
, for instance
t => tagIds.AsEnumerable().Contains(t.TagId)
or changing the type of the variable.
LINQ Breaking changes in EF Core 3.0. How can I compare strings without getting the warning CA1308?
The solution, as PanagiotisKanavos commented, was to simply use a.Name == b
. Easy and it works!
LINQ Expression cannot be translated
You are invoking C#-specific functionality in the WHERE
clause. Entity Framework is unable to convert it to pure SQL and is asking you to use AsEnumerable()
to filter in the app instead of trying to convert it to SQL.
You should be able to get to work simply but complying:
var data = await _context.Occurrence
.Include(o => o.Expense)
.AsEnumerable()
.Where(o =>
((o.RepeatStart.Date - date.Date).TotalHours % o.RepeatInterval == 0)
||
o.RepeatYear == date.Year || o.RepeatYear < 0
&&
o.RepeatMonth == date.Month || o.RepeatMonth < 0
&&
o.RepeatDay == date.Day || o.RepeatDay < 0
&&
o.RepeatWeek == ISOWeek.GetWeekOfYear(date) || o.RepeatWeek < 0
&&
o.RepeatWeekDay == (int)date.DayOfWeek || o.RepeatWeekDay < 0
&&
o.RepeatStart.Date <= date.Date
)
.Select(e => e.Expense)
.ToList();
This however, will download the entire Occurence table. You should restructure it as following:
var data = await _context.Occurrence
.Include(o => o.Expense)
.Where(o => /* SQL-friendly calls only */)
.AsEnumerable()
.Where(o => /* everything else */)
.Select(e => e.Expense)
.ToList();
Related Topics
How to Ignore Null Values for All Source Members During Mapping in Automapper 6
C# Inheritance and Default Constructors
How to Throttle Event Stream Using Rx
ASP.NET MVC 4 Multiple Post via Different Forms
How to Serialize an Exception Object in C#
How Do C# Events Work Behind the Scenes
Request Windows Vista Uac Elevation If Path Is Protected
File Getting Copied to Syswow64 Instead of System32
How to Get the Actual Monitor Name? as Seen in the Resolution Dialog
Convert System.Drawing.Icon to System.Media.Imagesource
What's So Bad About Building Xml with String Concatenation
What's the Best Way to Pass Event to Viewmodel