GroupBy method detaches entities
With Entity Framework Core, any queries which use a GroupBy statement results in detached entities. I am sure it is because of the GroupBy because I get tracked entities when I comment it out.
Steps to reproduce
I use the following query to get the distinct articles by the Code property:
var distinctArticles = await context.Articles
.GroupBy(
a => a.Code // String column
(key, articles) => articles.First() // FirstOrDefault gives the bug as well
)
.ToListAsync();
When I later on check the State of the entities, they are Detached. If I don't use the GroupBy my entities are (as expected) Unchanged and therefore correctly tracked:
foreach(var article in distinctArticles)
{
System.Diagnostics.Debug.WriteLine(context.Entry(article).State);
}
Perhaps this is intended behavior, but I couldn't find anything about it in the docs.
Further technical details
EF Core version: 2.2.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.2.0 Operating system: Windows 10 IDE: Visual Studio Enterprise 2017 15.8.7
Perhaps another variation for same bug as in #11762
Perhaps another variation for same bug as in #11762
This might very well be the case indeed, let me know if I should close this issue. When I looked at the generated SQL I noticed a lack of group by statement and only an order by statement which seems similar to your report.
Lack of Group By is by design on server side. SQL Group By is not same as Linq GroupBy. In above query GroupBy-FirstOrDefault is used. There is no SQL equivalent using Group By. But it can be translated using a different SQL. We haven't implemented it yet.
Duplicate of #12088