微风展开不能在EFAPI上使用WebAPI
我已经发布了一个WebAPI服务,它返回一个项目列表。我正在实施Breeze,并设法使其基本上与过滤/排序一起工作。但是,展开不起作用。
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
您可以在上面的响应中看到ToUserId ForeignKey,但是ToUser属性为NULL(用户肯定存在)
You can see the ToUserId ForeignKey in the response above, but the ToUser properties are NULL (the user definitely exists)
您可以看到ToUser EF导航属性元数据。
You can see the ToUser EF navigation property in the metadata.
当我在服务器端使用 .Include 时,我可以用EF填充它,但是我不想这样做。
When I use .Include on the server side I can populate it with EF, but I don't want to do this.
我在展开中查看了Breeze Tutorial 2: http: /learn.breezejs.com/
这是没有展开的: http: //learn.breezejs.com/api/northwind/Products
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
这里是展开(您可以看到其他类别信息): http://learn.breezejs.com/api/northwind/Products?$expand=Category
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...
更新:
我下载了微风1.3.6示例并在VS2011中加载DocCode解决方案。
我运行它,看到客户端扩展工作;
例如 http:// localhost:47595 / breeze / Northwind / Orders?$ top = 1
(不展开) http :// localhost:47595 / breeze / Northwind / Orders?$ top = 1& $ expand = Customer
(正确扩展客户)。
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).
我检查了WebAPI控制器代码,它看起来一样,除了使用EF Code First而不是Model First。外键装饰有一个属性:
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:
微风样品
[ForeignKey("CustomerID")]
[InverseProperty("Orders")]
public Customer Customer {get; set;}
它没有意义...这与我的WebAPI有关控制器或EntityFramework关系...
It just doesn't make sense... it is something to do with my WebAPI controller or EntityFramework relationship...
更新2
我下载了最基本的ToDo Knockout Breeze样本,并将此行添加到ToDoItem类: public User ToUser {get;组; }然后我可以使用http:// localhost:63030 / breeze / todos / Todos来扩展WebAPI调用
$ expand = ToUser
所以我得出结论,这与我使用EntityFramework DB First而不是代码优先的事实有关。在Breeze和EF的当前版本的WebAPI中确实看起来可以做到。
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.
更新3
我已经缩小它到我的数据库,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).
例如:如果你看看这个Expand在Breeze网站有效, http://learn.breezejs.com/api/northwind/产品?%24expand =类别,尝试将最后一个参数更改为无效字段,并引发错误,例如:
http://learn.breezejs.com/api/northwind / Products?%24expand = Category1
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
然而,在我的代码中,它总是忽略这个参数并返回所有记录,如果展开参数不正确:
http:// www .ftter.com / desktopmodules / framework / api / dare / dares?$ expand = To4657657User
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.
我的代码
[HttpGet]
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public HttpResponseMessage Dares()
{
var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable<Dare>)contextProvider.Context.Dares);
return ControllerUtilities.GetResponseWithCorsHeader(response);
}
这里是我的EF模型生成的类(使用Database First)
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; }
}
我想我已经找到问题 - HttpResponseMessage返回类型的IQueryable与纯IQueryable返回类型的行为不同。我没有包装时,扩展似乎工作。
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.
我在这里提出了一个新问题:
如何使用带有CORS的Breeze IQueryable?
I have raised a new question here: How to use Breeze IQueryable with CORS?