的WebAPI的OData与实体框架功能扩展 [英] Webapi odata expand with entity framework functions

查看:282
本文介绍了的WebAPI的OData与实体框架功能扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品ODATA控制器和产品类别ODATA控制器。结果
他们都是使用实体框架的实体,具有用于ODATA扩大导航方式。结果
这两个工作正常展开。结果
现在,我在实体框架增加了一个存储过程来处理从数据库返回的数据,仍然返回一个产品的纪录。结果
我设置实体存储过程函数返回类型为产品,并创造了产品ODATA控制器一个新的函数调用实体功能,然后返回产品。结果
我可以从一个url调用该函数,这是正确返回一个产品实体/ JSON。结果
现在,我需要调用的URL展开,以获得产品类别实体,但失败。

I have a Product odata controller and a Product Category odata controller.
They are both using entity framework entities and have navigation methods used for odata expand.
The expand for both is working fine.
Now I added a stored procedure in entity framework to manipulate the data returned from the database and still return a "Product" record.
I set the entity stored procedure function return type to "Product" and created a new function in the Product odata controller to call the entity function and return "Product".
I can call the function from a url and this is returning a Product entity / json correctly.
Now I need to call the expand on the url to get the "Product Category" entity but this fails.

我看着这篇文章,但是这是基于非实体模型。我的实体是正确并且运行正常。结果
http://beyondtheduck.com/projecting-and-the-odata-expand-query-option-possible-at-last-kinda/

I looked into this article but this is based on non-entity models. My entities are all correct and functioning fine.
http://beyondtheduck.com/projecting-and-the-odata-expand-query-option-possible-at-last-kinda/

推荐答案

下面是code我用来解决这个问题。结果
决不是正确的code。结果
例如:如果在包含ODataActionParameters一个动作用ODataQueryOptions.Top /跳跃将为空结果。
ODataActionParameters将包含顶部/跳过作为参数?很奇怪。结果
所以我都在希望,微软或其他人可以在将来解决这个问题。添加

Here is the code I used to fix the problem.
By no means is it "correct" code.
For example: ODataQueryOptions.Top / Skip will be null if used on an Action that contains ODataActionParameters.
ODataActionParameters will contain the Top / Skip as a parameter? Very odd.
So I added both in the hopes that Microsoft or someone else can fix this issue in the future.

控制器:

[HttpPost]
[EnableQuery]
public PageResult<SomeObject> SomeFunction(ODataQueryOptions<SomeObject> options, ODataActionParameters parameters)
{
    // Get the paging settings from ODataActionParameters since they are not shown on the ODataQueryOptions. Maybe there will be some fix for this in the future.
    int pageSize = (int)parameters["pageSize"];
    int take = (int)parameters["take"];
    int skip = (int)parameters["skip"];
    int page = (int)parameters["page"];

    // Apply page size settings
    ODataQuerySettings settings = new ODataQuerySettings();

    // Create a temp result set to hold the results from the stored procedure
    var tempResults = db.SomeStoredProc().ToList(); // ToList is required to get the "real" total count before paging

    // Apply the query options. For now this is only needed to get the correct count since the options does not seem to contain the TOP / SKIP when using OData parameters.
    IQueryable results = options.ApplyTo(tempResults.AsQueryable(), settings);

    // This was needed for custom paging. EXAMPLE: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
    return new PageResult<SomeObject>(tempResults.Skip(skip).Take(take),
                            Request.ODataProperties().NextLink,
                            Request.ODataProperties().TotalCount);
}

然后WebApiConfig:

Then WebApiConfig:

var SomeFunction = builder.Entity<SomeObject>().Collection.Action("SomeFunction");
SomeFunction.Parameter<int>("take");
SomeFunction.Parameter<int>("skip");
SomeFunction.Parameter<int>("page");
SomeFunction.Parameter<int>("pageSize");
SomeFunction.ReturnsCollectionFromEntitySet<SomeObject>("SomeObjects");

这篇关于的WebAPI的OData与实体框架功能扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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