使用OData的可查询与资料库? [英] using the OData Queryable with a repository?

查看:149
本文介绍了使用OData的可查询与资料库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个资源库的方法:

I have this repository method:

public IQueryable<TModel> GetAll()
{
    using (var context = new DatabaseContext())
    {
        return context.Set<TModel>().AsQueryable();
    }
}

其中,的TModel 是一个艺术家模型..反正

where TModel is an artist-model.. anyway

然后,我有我的控制器,这个动作:

And then I have this action in my controller:

// GET api/artist
[Queryable]
public IQueryable<ArtistModel> Get()
{
    return _repo.GetAll().AsQueryable();
}

现在..如果我想改变库方法返回一个列表,并添加 .ToList 我的结果..那么这将工作的伟大。但无论怎样的OData查询进来。我仍然会首先执行获取所有查询..把它变成一个列表,然后我就对这个列表执行我的OData查询。

Now.. if I would change the repository method to return a List and also add .ToList for my result.. then this would work great. But then no matter what OData query comes in.. I would still first execute a "get all query".. turn them into a list and then I would execute my OData query against that list..

这似乎只是简单的错误..什么我想这样做是为了确保OData的查询被执行为我试图从数据库中提取数据的同时..所以我只得到了非常具体的结果查询匹配的..而不是数据的一个巨大的一堆再后来就被质疑。

Which seems just plain wrong.. what I would like to do is to make sure that the OData query gets executed the same time as I'm trying to fetch data from the database.. so I only get the very specific result matching the query.. and not a huge bunch of data that then later on gets queried..

现在我与的DbContext 获取利用外部处理一次..但我仍然需要关闭问题的DbContext 还有一些地方,一些如何..

For now I have the problem with the DbContext getting disposed once outside the using.. but I still need to close the DbContext as well some where, some how..

任何想法?

推荐答案

网页API的可查询扩展的最新版本是一种令人困惑,因为它是从previous版本如此不同。在新版本中,你需要显式启用可查询的支持或使用新的类查询选项。 <一href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options\"相对=nofollow>看到这个

The latest version of the queryable extension of web api is kind of confusing, since it is so different from previous versions. In the new version you need to either explicitly enable queryable support or use the new query options class. See this

编辑:一些code这一点,因为我现在是在办公桌

Some code this since I am now at a desk

public IQueryable<TModel> GetAll(ODataQueryOptions opts)
    {
        var db = _repo.GetAll();
        return (IQueryable<TModel>) opts.ApplyTo(db);
    }

这篇关于使用OData的可查询与资料库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆