Skip to content

How to query navigation property of a singleton entity? #1532

@RonnyRen

Description

@RonnyRen

I want to support the following OData APIs, user is singleton, users is entity set, user has many orders, orders is entity set.
/odata/users: return info of all the users.
/odata/users({key}): return info of an user
/odata/user: return the info of current login user
/odata/orders: return all the orders
/odata/orders({key}): return an order based on the key
/odata/user/orders: return all the order of the current login user
/odata/user/orders({key}): return the order based on key of current login user

Class:
public class User
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
public ICollection Orders { get; set; }
}

public class Order
{
[Key]
public int Id { get; set; }
public string UserId { get; set; }
public string ProductName { get; set; }
public User User { get; set; }
}
EDM setting
builder.EntitySet("Users");
builder.Singleton("User");
builder.EntitySet("Orders");
// Navigation: User → Orders
builder.EntityType().HasMany(u => u.Orders);

Controllers
public class UsersController : ODataController
[EnableQuery]
public IActionResult Get() //works for /odata/users
public IActionResult Get([FromODataUri] string key) //works for /odata/users({key})

public class UserController : ODataController
[EnableQuery]
public IActionResult Get() //works for /odata/user

public class OrdersController : ODataController
[EnableQuery]
public IActionResult Get() //works for /odata/orders

[EnableQuery]
public IActionResult Get([FromODataUri] int key) //works for /odata/orders({key})

I can make route /odata/user/orders working through adding method GetOrders() in Controller UserController,
how to support both route /odata/user/orders and /odata/user/orders({key}) through standard OData API?
According to the Microsoft Copilot, it says OrdersController can handle routes /odata/orders, /odata/user/orders, /odata/orders({key}) and /odata/user/orders({key}) by default based on EDM setting, but it doesn't work in my testing, does OData support this case? What extra configure is required? Thanks.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions