-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EF Core 5 Table Valued Functions Unable to translate collection subquery in projection since the parent query doesn't project key columns of all Exception #23830
Comments
https://developercommunity2.visualstudio.com/t/Entity-Framework-50-LINQ-error-when-que/1286011 appears to be a dupe of this, but with a slightly different scenario which should likely be tested when fixing the issue. |
I'm experiencing this too. Queries that worked without any problems whatsoever in EF Core 3 don't work in EF Core 5 and give this error message. The error occurs when:
Note: I also used the workaround at the link in the comment above. I changed from |
I am experiencing the exact same issue. Is there a fix or work around for this? I am upgrading to EF 5 and cannot move passed this. I am not using distinct or group by, i am doing a union on two queries that return a type with a collection property (using "select new"). Removing the part of the query that sets the collection property prevents the error from happening but also does not solve my problem:
the break occurs here:
|
I have an update on this. This error seems to occur because the scaffolded view uses " entity.HasNoKey();" even though the view is using "ROW_NUMBER() OVER " in order to return a TempID. If i manually update the code produced by dbscaffold to "entity.HasKey" then my query works. The problem is that this is generated code so the next time a regenerate the dbcontext the change will be lost. I am trying to find a way on the sql side to tell EF core that the TempID column is a primary key.. still stuck on this. |
As mentioned above, I worked around this by setting a key in my model class. Previously it was keyless. No error showed up until I added a Include.ThenInclude in the query. |
This basically prevents the usage of |
Any updates on this? |
@shubhambothara @guidupuy This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 6.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you. Currently, this issue only has 3 votes. |
Note from triage: putting this in the 7.0 milestone to make sure we consider the design in 7.0. The outcome of that will determine together with prioritization against other work will determine whether or not this is implemented in 7.0. |
I had this bug as well in EF Core 5 with a similar query. To me the problem seems to be resolved in EF Core 6, maybe OP could verify. |
@dIeGoLi I have bad news... |
Can confirm |
In a LINQ query, the result can contain a complex structure which while have scalar values, it can also contain collection of scalar or non-scalar values. Since relational database result which uses row cannot contain anything other than scalar values, in order to generate results, EF Core has to iterate over multiple rows to generate single result in client side. There is extensive discussion around how to get related data in multiple rows for the query in single DbCommand vs using multiple DbCommands (single vs split query mode).
While 2 is very obvious, 1 & 3 may not very straight-forward to understand why they are required. So let's look at some queries, In both cases, we need to identify each outer/inner element individually to correctly generate result. For an entity type in EF Core which has PK defined, the identifying column could be the set of primary key. When query has a join between 2 different tables, we can uniquely identify each row in the result by combining primary key of both the tables. This whole thing doesn't work for keyless entity, because they don't have key. Regardless of collection result, in normal case, we cannot say if any row is duplicated in keyless entity. A keyless entity which is mapped to a view in database, can actually have multiple rows which exactly same set of values (essentially duplicate), but that is fine in normal case since each row is separate instance of keyless entity and we materialize as such. For people saying that it worked correctly in 3.1, In both the linked issue, user has explicitly configured one of the entity type participating in join to be keyless. Hence it throws exception. If you have a unique way of identify each row for that entity type then configure that as primary key or rewrite query to perform join and collection projection in 2 separate queries so you can do stiching of parts manually based on your domain model what it makes sense. |
Hello,
I get an Exception in EF Core 5 Table when I use: Table Valued Functions :
InvalidOperationException: Unable to translate collection subquery in projection since the parent query doesn't project key columns of all of it's tables which are required to generate results on client side. This can happen when trying to correlate on keyless entity or when using 'Distinct' or 'GroupBy' operations without projecting all of the key columns.
// Entities
// App DB Context
The text was updated successfully, but these errors were encountered: