Search code examples
asp.net-coreodata

AspNetCore 2 oData - Missing context on get all identity users


I trying to create a web api with oData v4.

Now i try to get all Identity-User over oData.

This is working:

[EnableQuery]
public class UsersController : Controller
{
    protected readonly UserManager<User> _userManager;

    public UsersController(UserManager<User> userManager)
    {
        _userManager = userManager;
    }

    private static List<User> _users = new List<User>
    {
        new User { Id = 1, Name = "Flo", Email = ""},
        new User { Id = 2, Name = "Felix", Email = ""},
        new User { Id = 3, Name = "Andreas", Email = ""},
        new User { Id = 4, Name = "Marco", Email = ""}
    };

    public IQueryable<User> Get()
    {
        return _users.AsQueryable();
    }

}

And return this response:

{"@odata.context":"http://localhost:55503/oData/$metadata#Users(Id)","value":[{"Id":1},{"Id":2},{"Id":3},{"Id":4}]}

When i change the controller to return all Identity-Users this isn't working correctly.

[EnableQuery]
public class UsersController : Controller
{
    protected readonly UserManager<User> _userManager;

    public UsersController(UserManager<User> userManager)
    {
        _userManager = userManager;
    }

    public IQueryable<User> Get()
    {
        return _userManager.Users.AsQueryable();
    }

}

And it returns this response:

[{"Id":"35909773-8b53-4d68-a770-b7cdfcffd0de"}]

But the response is missing the context. Can you give my a hint why?


Solution

  • I solved the problem:

            var builder = new ODataConventionModelBuilder(serviceProvider);
    
            builder.EntitySet<User>("Users");
    
            return builder.GetEdmModel();