-
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
Invalid query produced when projecting DTO containing subquery that returns a single element #7714
Comments
/cc @maumar |
Tried to use vs 2017. Migrated projects, all compiled, but when try to get data from database I've got System.InvalidOperationException: 'No value provided for required parameter '_outer_Id'.'
For queries without getting data from navigation properties all is good. All EFcore versions is latest stable 1.1.1(0)(Like mentioned above). VS 2017 Release. In VS 2015 have no problem. Update: I've just realize that's a huge step back, if for 1 entity I can made some additional queries, but I have similar queries that return list, I now I should made additional request for every entity, that a huge overhead. |
The same thing for me:
If I change the code to create an anonymous class like that:
Works as usual. The Error appeared after upgrading to VS 2017. UPDATE:
|
I have the same error in VS 2017. Temporary solution with automapper work with simple Count, but in my case i have count of group items, somth like this: In this case i have same error: '_outer_Id' with anonymous class too. In VS2015 all work fine. |
@maumar It looks like this could be a dupe of or related to #7811 and is probably also a regression from 1.1.0. As Doug originally reported it there was a query compiler error in 1.1.0 and hence we didn't think it was a regression, but from other comments it seems like there were cases that were working in 1.1.0. I'm putting this in 1.1.2 for now, but feel free to close as a dupe or set for re-triage as appropriate. |
I confirm what in 1.1.0 with VS 2017 all work correct |
…ase the blocking issue dotnet/efcore#7714
I'm getting a similar error but I'm selecting an anonymous type. This broke after upgrade to VS 2017 and EF Core 1.1.1. |
Problem was caused by: b8fb258 |
@joshmouch can you post your query and model? (entities and contents of OnModelCreating method on the DbContext) |
InvalidOperationException
at runtime for simpler version of #6534 query…subquery that returns a single element Problem was that in the fix for #7033 we force client evaluation on queries that try to bind to parents, when the parent has a client projection. However, for the case when single element is returned (e.g. cusotmers.Select(c => new DTO { Count = c.Orders.Count() })) we don't need to do client evaluation. We actually try to merge the subquery into the parent query, but this causes a problem because earlier in the pipeline we assumed that client eval is needed (since parent QM has client projection) Fix is to be consistent and never try to lift subquery if the parent QM has a client projection. This causes more scenarios to produce N+1 queries, but the proper fix would require a breaking change: we need additional information passed to the SqlTranslatingExpressionVisitor to know that the subquery that is being visited returns a single value.
Fixed in 8dbcab1 |
Also opened #7882 to track proper fix in the 2.0.0 that would not introduce unnecessary N+1 queries for those cases. |
@maumar When you say fixed, you mean it will be fixed with the next entity framework update? Do you have a date approximatively? Thanks |
@paddyfink the fix will be available with the next patch release (1.1.2). Unfortunately we don't have any dates yet. If possible, you should use the workaround, i.e. don't use the DTO, but use anonymous type instead (and then project into DTO in memory): rather than: customers.Select(c => new MyDTO { Count = c.Orders.Count() }).ToList(); do: customers.Select(c => new { Count = c.Orders.Count() }).ToList().Select(r => new MyDTO { Count = r.Count }); If this workaround works for you, you should use it even after the fix is in, as the queries with DTO |
@joshmouch as @maumar said they can't fix that bug without changing API. EF supports semantic versioning and breaking change such as changing API means a major change from point of semantic versioning. Because it ruins backward compatibility. Major change means increasing the first number of the version => 2.0. |
I have following query with the same error message: var test = _repository.AggregateQueryable
.Select(x => x.Items.Any()
? x.Items.GroupBy(y => y.Priority).Select(g => g.Key).ToArray()
: new int[0]); What is the workaround for this? |
…ntaining subquery that returns a single element Problem was that in the fix for #7033 we force client evaluation on queries that try to bind to parents, when the parent has a client projection. However, for the case when single element is returned (e.g. cusotmers.Select(c => new DTO { Count = c.Orders.Count() })) we don't need to do client evaluation. We actually try to merge the subquery into the parent query, but this causes a problem because earlier in the pipeline we assumed that client eval is needed (since parent QM has client projection) Fix is to flow information about query model's StreamedDataInfo into SqlTranslatingExpressionVisitor so that we only client-eval when it's really necessary (i.e. when more than one element is returned from the subquery). Information is stored in (proper) internal fields as we don't really want to expose this API in any way.
Seems like I also met same error when trying to get some aggregated values in subselect
with models
test project if needed |
@maumar can you please explain if error I have is related to this error as suggested workaround seems like does not work in one case
Error is also
generates |
@dvdobrovolskiy the scenario from your first post is indeed the same problem. The proposed workaround should resolve it though (tested with the model you provided). The second issue is different - it's a compile time error, where the issue in question throws at runtime (due to incorrect SQL being generated). Could you file a new issue for this? Please also provide the model used, as it seems to be different (more complex) than the one you provided. |
New issue #8005 PS |
…ntaining subquery that returns a single element Problem was that in the fix for #7033 we force client evaluation on queries that try to bind to parents, when the parent has a client projection. However, for the case when single element is returned (e.g. cusotmers.Select(c => new DTO { Count = c.Orders.Count() })) we don't need to do client evaluation. We actually try to merge the subquery into the parent query, but this causes a problem because earlier in the pipeline we assumed that client eval is needed (since parent QM has client projection) Fix is to flow information about query model's StreamedDataInfo into SqlTranslatingExpressionVisitor so that we only client-eval when it's really necessary (i.e. when more than one element is returned from the subquery). Information is stored in (proper) internal fields as we don't really want to expose this API in any way.
@szykov @joshmouch good news, we did some extra digging around this issue and were able to come up with a different, more functional approach. Once the patch is live, you should be able to use DTOs without drawbacks, just like in 1.1.0. Query from #7915 will also be better, although there will still be N+1 due to other issues (discussed in the bug itself) |
I agree that this is a pretty serious bug - especially considering VS 2017 is a one-way upgrade. Can we grab a nightly build that includes this fix anywhere? |
@mb2140 we are planning to provide a feed with the patch fixes, however cureently we don't know when that will be available. For the time being you have to clone the patch repo (https://github.com/aspnet/EntityFramework/tree/rel/1.1.2) and build it manually. |
This patch fix is approved. Please follow the normal code review / pull request process and make sure you make the change in the correct branch. |
Preview builds of this patch fix should be available on the following feeds:
If you have a chance, please try out these preview builds and let us know if you have any feedback! |
Patch did`nt helped with #8233 |
@dvdobrovolskiy - The probable fix for #8233 is not in patch and will be released in 2.0.0-preview1 |
Year, may the 10th. Waiting. Thanks! |
@Eilon My project was hit by this bug with 1.1.1 so I have have kept it at EfCore 1.1.0. |
@andershermansen that's great, thank you so much for trying it out! |
… tooling becuase the blocking issue dotnet/efcore#7714
…ase the blocking issue dotnet/efcore#7714
…ase the blocking issue dotnet/efcore#7714
…ase the blocking issue dotnet/efcore#7714
Describe what is not working as expected.
Using a simplified version of the #6534 code, get an
InvalidOperationException
instead of expected compilation failure (pre-1.1.1) or result (with 1.1.1). Main simplification is this one does not involve an optional (1 => 0,1
) relationship.Using the out-of-the-box application database created in a .NET Core web application with Individual User Accounts, add the following to
HomeController
:Then, restore, build and run the application. Navigate to
/home/bug
and see the stack on the console.If you are seeing an exception, include the full exceptions details (message and stack trace).
Steps to reproduce
See description above.
Further technical details
EF Core version:
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 RC
The text was updated successfully, but these errors were encountered: