You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GroupBy with Rank on Owned Entity property not working #31062
GroupBy with Rank on Owned Entity property not working
I have entities where owned entity is used to store dates in my custom format. They are configured through Model Builder and configuration class (сonfiguration with [NotMapped] annotation behaves similarly), and produce in database two fields: varchar - SomeName_Date and integer SomeName_DateAccuracy.
Simple OrderBy without GroupBy working well, but when i trying to get only last row per grouping field and use GroupBy(x=> x.SomeField) .Select(x => x.OrderByDescending(y => y.StartDate.Date).First()) - i got translation failed. Same code with group, select and order by simple properties works well.
Example
For a reproducible example, i modified GetStarted a bit from the documentation to show exactly what is not working and what is working. GetStarted.zip
Code
This GroupBy will not work and produce Translation failed:
Translation of 'EF.Property<CustomDate>(EntityShaperExpression:
Post
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
, "StartDate")' failed. Either the query source is not an entity type, or the specified property does not exist on the entity type.
Provider and version information
EF Core version: 7.0.5 (and 8.0.0-preview.4)
Database provider: Pomelo.EntityFrameworkCore.MySql 7.0.0 / Microsoft.EntityFrameworkCore.Sqlite 7.0.5
Target framework: NET 7.0
Operating system: Windows 11, Ubuntu 22.04.2
IDE: Visual Studio 2022 17.5.4
usingvardb=newBloggingContext();db.Database.EnsureDeleted();db.Database.EnsureCreated();varblog=newBlog{Url="http://blogs.msdn.com/adonet",Posts={newPost{Title="Hello World",Content="I wrote an app using EF Core!",StartDate=newCustomDate{Date="0014-01-1990"}},newPost{Title="Hello World second",Content="I wrote an app using EF Core!",StartDate=newCustomDate{Date="0017-01-1990"}}}};varsecondblog=newBlog{Url="http://blogs.msdn.com/adonet",Posts={newPost{Title="Hello World third",Content="I wrote an app using EF Core!",StartDate=newCustomDate{Date="0014-02-1995"}}}};db.AddRange(blog,secondblog);db.SaveChanges();varbrokenGroupByBlogId=db.Posts.GroupBy(x =>x.BlogId).Select(x =>x.OrderByDescending(y =>y.StartDate.Date).First());foreach(varpostinbrokenGroupByBlogId){Console.WriteLine(post.Title+" - "+post.BlogId+" - "+post.StartDate.Date);}publicclassBloggingContext:DbContext{publicDbSet<Blog>Blogs{get;set;}publicDbSet<Post>Posts{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptions)=>options.UseSqlite("Data Source=test.db").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}publicclassBlog{publicintBlogId{get;set;}publicstringUrl{get;set;}publicList<Post>Posts{get;}=new();}publicclassPost{publicintPostId{get;set;}publicstringTitle{get;set;}publicstringContent{get;set;}publicCustomDateStartDate{get;set;}publicintBlogId{get;set;}publicBlogBlog{get;set;}}[Owned]publicclassCustomDate{publicstringDate{get;set;}}
Activity
ajcvickers commentedon Jun 19, 2023
Note for triage: this is a regression in EF7. SQL generated by EF Core 6:
Code:
TeckTn commentedon Dec 22, 2023
Is there any workaround for this bug ?
stevendarby commentedon Dec 22, 2023
Regression tag? Servicing consider?
Santehnikua commentedon Dec 22, 2023
I managed this by using FromSqlRaw and writing the necessary query manually...
DPDmancul commentedon Jul 9, 2024
@ajcvickers This is still bugged in EF 8 and using raw sql queries could not be considered a workaround for this impacting regression
Query: expand owned navigations in GroupBy aggregate selectors
Query: expand owned navigations in GroupBy aggregate selectors
Query: expand owned navigations in GroupBy aggregate selectors (dotne…
Query: expand owned navigations in GroupBy aggregate selectors (#38918)