WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Expand not working with EnableQuery and PageSize

Open marcelmunarolo opened this issue 6 years ago • 8 comments

The command "expand" with two levels are not working properly using the given scenario: Order (HasMany)-> ProductsSold (HasOne)-> Product We also observed that it is related with the EnableQuery attribute with PageSize informed.

Assemblies affected

TargetFramework: netcoreapp3.1 Microsoft.AspNetCore.OData (7.3.0) Microsoft.OData.Core (7.6.1) Microsoft.EntityFrameworkCore (3.1.1)

Reproduce steps

Please, clone and execute the following repo in order to reproduce the error: git clone https://github.com/marcelmunarolo/ODataExpandIssue.git

In resume: ... [EnableQuery(PageSize = 100)] // Do not work //[EnableQuery] // Works public virtual IQueryable Get() ...

Expected result

By using the following query: http://localhost:5001/odata/OrderQuery?$expand=ProductsSold($expand=Product) we expect to receive the following structure: ... { "Id": 1, "Shipment": "Fedex", "ShipmentValue": 12.00, "Discount": 2.90, "Customer_Id": 1, "ProductsSold": [ { "Id": 1, "Order_Id": 1, "Product_Id": 1, "Price": 42.90, "Quantity": 1, "Product": { "Id": 1, "Name": "test product", "Price": 0.01, "StartPromotion": "2020-06-01T00:00:00Z", "EndPromotion": "2020-07-01T00:00:00Z", "Category_Id": 1 } } ] } ...

Actual result

The following error is occurring: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Product_Id'. Invalid column name 'Order_Id'. Invalid column name 'Price'. Invalid column name 'Product_Id'. Invalid column name 'Quantity'. ...

However, The following queries are working properly, even with the PageSize informed: http://localhost:5001/odata/OrderQuery?$expand=ProductsSold,Customer http://localhost:5001/odata/OrderQuery?$expand=ProductsSold,Customer($expand=CustomerAddresses)

Thanks!

marcelmunarolo avatar Apr 01 '20 12:04 marcelmunarolo

I wasn't able to get your project running. I get the error message:

Microsoft.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No such host is known.)

RobbeSchepens avatar Apr 01 '20 13:04 RobbeSchepens

In order to execute the project, you need the Docker Desktop running. In the Visual Studio, set the docker-compose project as the Start Up project, and hit Ctrl+F5. Or via CLI, in the project folder, you can use "docker-compose up --build".

In case you don't have the Docker Desktop, you can use an existing SQL Server instance, by adjusting the connection string in the web project.

Please, let me know if you need any other info. Cheers!

marcelmunarolo avatar Apr 01 '20 13:04 marcelmunarolo

I used my SQL Express by setting appsettings.json connectionstring to "ECommerce": "Server=(LocalDb)\\MSSQLLocalDB;Database=ECommerce".

If you use [EnableQuery(EnableCorrelatedSubqueryBuffering = true, PageSize = 100)] it will work. But I actually don't really know why it works like that. Maybe someone with more knowledge about the ins and outs of OData and queries can explain.

RobbeSchepens avatar Apr 01 '20 13:04 RobbeSchepens

Yes, if I use [EnableQuery(EnableCorrelatedSubqueryBuffering = true, PageSize = 100)] it works, however, queries like this $expand=ProductsSold,Customer($expand=CustomerAddresses) stop working.

The following error occurs: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

marcelmunarolo avatar Apr 02 '20 14:04 marcelmunarolo

@marcelmunarolo @xuzhg @mikepizzo It would appear this issue is related to an EF Core issue that we are tracking where expressions generated by OData execute successfully with EF6 but fail with EF Core. I created a replica project using EF6 and verified that the same expression works. I even went ahead tested with EF Core 5.0.0 RC2 and confirmed that it DOES NOT resolve the issue.

gathogojr avatar Oct 28 '20 07:10 gathogojr

Repros - EFCore and EF6 side-by-side https://github.com/gathogojr/ODataWebApiIssue2106Repro

gathogojr avatar Oct 28 '20 08:10 gathogojr

Hey, we are experiencing a similar issue, but upgrading to EF Core 5.0.1 seems to fix it, might be worth while to review this?

orbus-dkreivenas avatar Jan 26 '21 17:01 orbus-dkreivenas

This issue was due to a known issue that was fixed in EF Core 6.0 Project demonstrating that the issue was fixed added to the repro repo: https://github.com/gathogojr/ODataWebApiIssue2106Repro/tree/master/src/ODataWebApiIssue2106Repro.Net6 README: https://github.com/gathogojr/ODataWebApiIssue2106Repro/tree/master

gathogojr avatar Jun 07 '23 08:06 gathogojr