Repository navigation
Closed
Labels
Servicing-approvedarea-querypriority-bugIssues which requires API breaks and have bigger impact hence should be fixed earlier in the releaseregression
Milestone
Description
Code below works in EF Core 6.0.
var query = context.Books
.GroupBy(post => post.Author.Id)
.Select(grouping => new
{
Author = grouping.Key,
TotalCost = grouping.Sum(post => post.Detail.Price),
});Unhandled exception. System.InvalidOperationException: Translation of 'EF.Property<BookDetail>(EntityShaperExpression:
Book
ValueBufferExpression:
ProjectionBindingExpression: Outer
IsNullable: False
, "Detail")' failed. Either the query source is not an entity type, or the specified property does not exist on the entity type.
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMember(MemberExpression memberExpression)
at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateAggregateMethod(EnumerableExpression enumerableExpression, MethodInfo method, List`1 scalarArguments)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TryTranslateAggregateMethodCall(MethodCallExpression methodCallExpression, SqlExpression& translation)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.VisitNew(NewExpression newExpression)
at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at Program.Main() in C:\local\code\AllTogetherNow\Daily\Daily.cs:line 103
public class Author
{
public int Id { get; set; }
public string Name { get; set; } = default!;
}
public class Book
{
public int Id { get; set; }
public Author Author { get; set; } = default!;
public BookDetail Detail { get; set; } = default!;
}
[Owned]
public class BookDetail
{
public int Price { get; set; }
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.ConnectionString)
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
public DbSet<Book> Books => Set<Book>();
public DbSet<Author> Authors => Set<Author>();
}
public class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var toast = new Author { Name = "Toast" };
var alice = new Author { Name = "Alice" };
context.AddRange(
new Book { Author = alice, Detail = new() { Price = 10 } },
new Book { Author = alice, Detail = new() { Price = 10 } },
new Book { Author = toast, Detail = new() { Price = 12 } },
new Book { Author = toast, Detail = new() { Price = 12 } },
new Book { Author = toast, Detail = new() { Price = 14 } });
context.SaveChanges();
}
using (var context = new SomeDbContext())
{
var query = context.Books
.GroupBy(post => post.Author.Id)
.Select(grouping => new
{
Author = grouping.Key,
TotalCost = grouping.Sum(post => post.Detail.Price),
});
foreach (var group in query)
{
Console.WriteLine(group);
}
}
}
}Metadata
Metadata
Assignees
Labels
Servicing-approvedarea-querypriority-bugIssues which requires API breaks and have bigger impact hence should be fixed earlier in the releaseregression
Activity
smitpatel commentedon Sep 26, 2022
ajcvickers commentedon Sep 27, 2022
Note from triage: this is the result of a design change in query and cannot easily be fixed. We will document as a breaking change in 7.0.
MoazAlkharfan commentedon Sep 28, 2022
Is this a workaround? if it is, it doesn't seem to work on json columns too :(
ajcvickers commentedon Sep 30, 2022
@smitpatel The workaround query still throws the same exception.
smitpatel commentedon Sep 30, 2022
Exception only happens for owned type since when expanding lambdas over aggregate function, we don't expand owned navigations (which are not expanded by nav expansion). There is no way to expand beforehand, since it is 2nd level lambda. The expansion in subquery form would happen automatically when translating but the issue here is that we end up throwing exception from
EF.Propertybefore we conclude that it cannot be translated in aggregate form. To fix this, we should stop throwing directly in https://github.com/dotnet/efcore/blob/main/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs#L729 rather use error details which will revert the query back to subquery form translation (what happens for non-owned navigation).For better fix in future, we should expand out the navigation from sql translator and try to put in aggregate only if it didn't cause additional table to be added else go to subquery form. (Till we figure out how to lift join tables.
Query: Don't fail translation of aggregate when owned navigations
Query: Don't fail translation of aggregate when owned navigations
Query: Don't fail translation of aggregate when owned navigations
Dwipraj commentedon May 18, 2024
I am not sure what I am doing wrong, below code is throwing the following error.
I am using EF Core 8.0.4
QUERY
MODEL CLASS
ERROR
roji commentedon May 18, 2024
@Dwipraj please open a new issue for that.
Dwipraj commentedon May 19, 2024
Ok, but it seems I am missing something with the
Count()method because replacing Count withSum()not throwing any exception and also returns the result as expected.Should I open a new issue for that or not? @roji