AspNetCore 2 oData - 获取所有身份用户时缺少上下文

时间:2018-03-10 19:27:55

标签: asp.net-core odata

我尝试用oData v4创建一个web api。

现在我尝试通过oData获取所有Identity-User。

这是有效的:

[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();
    }

}

并回复此回复:

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

当我更改控制器以返回所有身份用户时,这不能正常工作。

[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();
    }

}

它会返回此回复:

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

但是回应缺少了背景。你能给我一个提示吗?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

        var builder = new ODataConventionModelBuilder(serviceProvider);

        builder.EntitySet<User>("Users");

        return builder.GetEdmModel();