Count query issues 2 database calls
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Description
Assemblies affected
Microsoft.AspNetCore.OData 8.0.12
Describe the bug
When I call to this action:
/odata/Orders?$count=true&$top=0&$skip=0
I get an answer that is correct:
{"@odata.context":"https://gridblazorodata.azurewebsites.net/odata/$metadata#Orders","@odata.count":1031,"value":[]}
But the database is called twice:
- to get the count value:
SELECT COUNT_BIG(*) FROM [Orders] AS [o] - to get the the records, that in this case are null:
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[Freight], [o].[OrderDate], [o].[RequiredDate], [o].[ShipAddress], [o].[ShipCity], [o].[ShipCountry], [o].[ShipName], [o].[ShipPostalCode], [o].[ShipRegion], [o].[ShipVia], [o].[ShippedDate] FROM [Orders] AS [o] WHERE 0 = 1
The second database query should not be issued because the $top parameter is 0.
Reproduce steps
A simple controller to any table will reproduce this behavior:
public class OrdersController : ODataController
{
private readonly NorthwindDbContext _context;
public OrdersController(NorthwindDbContext context)
{
_context = context;
}
[EnableQuery]
public IActionResult Get()
{
var repository = new OrdersRepository(_context);
var orders = repository.GetAll();
return Ok(orders);
}
}
Data Model
I use the Order table of the Northwind sample database
public partial class Order
{
public Order()
{
this.OrderDetails = new HashSet<OrderDetail>();
}
[Key]
[Required]
public int OrderID { get; set; }
[Required]
public string CustomerID { get; set; }
[Required]
public int? EmployeeID { get; set; }
public DateTime? OrderDate { get; set; }
public DateTime? RequiredDate { get; set; }
public DateTime? ShippedDate { get; set; }
public int? ShipVia { get; set; }
public decimal? Freight { get; set; }
public string ShipName { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public string ShipRegion { get; set; }
public string ShipPostalCode { get; set; }
public string ShipCountry { get; set; }
[ForeignKey("CustomerID")]
public virtual Customer Customer { get; set; }
[ForeignKey("EmployeeID")]
public virtual Employee Employee { get; set; }
[JsonIgnore]
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
[ForeignKey("ShipVia")]
public virtual Shipper Shipper { get; set; }
}
EDM (CSDL) Model
public static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Order>("Orders");
builder.EntitySet<Order>("OrdersWithErrors");
builder.EntitySet<OrderDetail>("OrderDetails");
builder.EntitySet<Customer>("Customers");
builder.EntitySet<Employee>("Employees");
builder.EntityType<Employee>().Ignore(r => r.Photo);
builder.EntityType<Employee>().Property(r => r.Base64String);
builder.EntitySet<Shipper>("Shippers");
builder.EntitySet<Product>("Products");
return builder.GetEdmModel();
}
Expected behavior
A call to get only the number of records should only issue one database query, and no other query to get rows should be issued
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the [EnableQuery] action and the OData query-processing path for $count=true&$top=0&$skip=0. Reproduce the request against the Orders controller and inspect why the count query is followed by a WHERE 0 = 1 row query. Done means the response remains correct while only the count database query is issued.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100