微风展开不工作的WebAPI与EF [英] Breeze Expand not working on WebAPI with EF

查看:138
本文介绍了微风展开不工作的WebAPI与EF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发表它返回的项目的列表一个的WebAPI服务。我采取微风,并设法得到它基本上是与筛选工作/排序。但是,展开不工作。

http://www.ftter.com/desktopmodules /框架/ API /敢/敢?$ =拓展TOUSER

您可以看到ToUserId ForeignKey的在上面的反应,但TOUSER属性是NULL(用户肯定存在)

您可以看到在元数据中TOUSER EF导航属性。

当我使用 .INCLUDE 在服务器端,我可以使用EF填充它,但我不希望这样做。

我查了微风教程2在这里展开: http://learn.breezejs.com/
下面是它没有扩大: http://learn.breezejs.com/api/northwind/Products

和在这里它与展开(你可以看到更多的类别信息)的 http://learn.breezejs.com/api/northwind/Products?$expand=Category

这就是我要做的,但我的不填吧...

更新:
我下载了微风1.3.6样品,并加载VS2011的文件code解决方案。
我跑了它,看到客户端扩展工程;
例如
的http://本地主机:47595 /微风/罗斯文/订单$顶部= 1 (不展开)
的http://本地主机?:47595 /微风/罗斯文/订单$顶部= 1&安培; $展开;客户(正确扩展客户)

我检查的WebAPI控制器code和它看起来一样的,除了他们使用EF code首先,而非模型优先。外键装饰有一个属性:

微风样的作品

  [ForeignKey的(客户id)]
[InverseProperty(订单)]
公众客户客户{搞定;组;}

这只是没有任何意义......它是与我的WebAPI控制器或关系的EntityFramework ...

更新2
我下载了最基本的待办事项淘汰赛微风样本,并添加此行到内的TodoItem类:公众用户至用户{搞定;组; }
我就能够展开的WebAPI调用以http://本地主机:63030 /微风/待办事项/托多斯$扩大TOUSER =

所以我得出的结论,这是事做的事实,我现在用的EntityFramework DB第一,而不是code首先。这肯定似乎可能在微风和EF的的WebAPI的当前版本做的。

更新3
我已经把范围缩小到我的数据库,数据库EF第一,code首先分歧,但仍然没有确定的问题。我从一个模式更改为code首先与方法完全相同的结果(即没有展开)。

例如:如果你看一下这个展开的微风网站的作品,的 http://learn.breezejs.com/api/northwind/Products?%24expand=Category ,尝试最后的参数更改为无效字段,它抛出一个错误,例如:
http://learn.breezejs.com/api/northwind/Products?%24expand =组别

不过,在我的code,它总是会忽略此参数和返回所有的记录,如果扩展参数不正确,不会抛出一个异常:
http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=To4657657User

所以我难倒。我不知道为什么,这是行不通的。

我的code

  [HTTPGET]
    [可查询(AllowedQueryOptions = AllowedQueryOptions.All)
    公众的Htt presponseMessage敢()
    {
        VAR响应= Request.CreateResponse(的HTTPStatus code.OK,(IQueryable的<&敢GT;)contextProvider.Context.Dares);
        返回ControllerUtilities.GetResponseWithCorsHeader(响应);
    }

和这里是从我的EF模型生成的类(使用数据库优先)

 公共部分类胆敢
{
    公众诠释DareId {搞定;组; }
    公众诠释ToUserId {搞定;组; }
    公共虚拟用户至用户{搞定;组; }
}


解决方案

我想我已经找到了问题 - IQueryable的同型Htt presponseMessage返回类型不具有相同的行为作为一个纯粹的IQueryable返回类型。扩展似乎工作的时候,我不换了。

我在这里提出一个新的问题:
如何使用IQueryable的微风与CORS?

I have published a WebAPI service which returns a list of items. I am implementing Breeze and have managed to get it basically working with filtering/sorting. However, the Expand is not working.

http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=ToUser

You can see the ToUserId ForeignKey in the response above, but the ToUser properties are NULL (the user definitely exists)

You can see the ToUser EF navigation property in the metadata.

When I use .Include on the server side I can populate it with EF, but I don't want to do this.

I checked the Breeze Tutorial 2 here on Expand: http://learn.breezejs.com/ Here is it without expand: http://learn.breezejs.com/api/northwind/Products

and here it is with Expand (And you can see the additional Category info): http://learn.breezejs.com/api/northwind/Products?$expand=Category

This is what I am trying to do but mine does not fill it...

UPDATE: I downloaded the Breeze 1.3.6 Samples and loaded the DocCode solution in VS2011. I ran it and saw that the client-side expand works; e.g. http://localhost:47595/breeze/Northwind/Orders?$top=1 (no expand) http://localhost:47595/breeze/Northwind/Orders?$top=1&$expand=Customer (expands customer correctly).

I checked the WebAPI controller code and it looks the same, except they use EF Code First instead of Model First. The Foreign key is decorated with a property:

Breeze Sample that Works

[ForeignKey("CustomerID")]
[InverseProperty("Orders")]
public Customer Customer {get; set;}

It just doesn't make sense... it is something to do with my WebAPI controller or EntityFramework relationship...

UPDATE 2 I downloaded the most basic ToDo Knockout Breeze sample and added this line to the ToDoItem class: public User ToUser { get; set; } I am then able to Expand the WebAPI call with http://localhost:63030/breeze/todos/Todos?$expand=ToUser

So I have come to the conclusion that it is something to do with the fact that I am using EntityFramework DB First and not Code First. It definitely does seem possible to do in the current version of the WebAPI with Breeze and EF.

UPDATE 3 I have narrowed it down to my database, EF Database First and Code First differences, but still not identified the issue. I have changed from a Model to a Code First approach with the exact same result (ie. no expand).

For example: if you look at this Expand on the Breeze site that works, http://learn.breezejs.com/api/northwind/Products?%24expand=Category, try change the last param to an invalid field and it throws an error, e.g. : http://learn.breezejs.com/api/northwind/Products?%24expand=Category1

However, in my code, it always ignores this param and returns ALL the records, and never throws an exception if the Expand param is incorrect: http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=To4657657User

Hence I am stumped.. I have no idea why this is not working.

My Code

    [HttpGet]
    [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public HttpResponseMessage Dares()
    {
        var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable<Dare>)contextProvider.Context.Dares);
        return ControllerUtilities.GetResponseWithCorsHeader(response);
    }

and here is the generated class from my EF model (using Database First)

public partial class Dare
{
    public int DareId { get; set; }
    public int ToUserId { get; set; }
    public virtual User ToUser { get; set; }
}

解决方案

I think I have found the problem - the IQueryable with the HttpResponseMessage return type does not behave the same as a pure IQueryable return type. expand seems to work when I do not wrap it.

I have raised a new question here: How to use Breeze IQueryable with CORS?

这篇关于微风展开不工作的WebAPI与EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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