I upgraded efcore 2.2 to 5 and simple group by is not working,
the data is already in memory, take a look:
List<IGrouping<Categories, Businesses>> businessesByCategory =
location.Businesses
.GroupBy(x => x.Category.Parent ?? x.Category)
.ToList();
In Ef Core 2.2 it worked fine, the businesses were grouped by their category, now it does nothing.
If I try to group by id it works:
List<IGrouping<int, Businesses>> businessesByCategory = location.Businesses
.GroupBy(x => x.Category.ParentId ?? x.CategoryId)
.ToList();
But I need the Category entity and this way I get only the category id.
The issue is caused by EF Core 3.0 no-tracking query behavior breaking change - No-tracking queries no longer perform identity resolution. Because of that, the
Category
objects with the sameId
now are different instances, hence the default equality comparer used by LINQ to ObjectsGroupBy
treats them as different keys, thus not grouping at all.EF Core 5.0 brings back the 2.x behavior - No-tracking queries with identity resolution, but you must opt-in for it using the
AsNoTrackingWithIdentityResolution
method in place ofAsNoTracking()
. OrQueryTrackingBehavior.NoTrackingWithIdentityResolution
if setting the defaultChangeTracker.QueryTrackingBehavior
.