Breeze Expand not working on WebAPI with EF

二次信任 提交于 2019-12-10 17:07:41

问题


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; }
}

回答1:


Your URL seems to be missing the $ for the expand query option...should be $expand.




回答2:


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?



来源:https://stackoverflow.com/questions/17399906/breeze-expand-not-working-on-webapi-with-ef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!