Note that there are some explanatory texts on larger screens.

plurals
  1. POBreeze Expand not working on WebAPI with EF
    primarykey
    data
    text
    <p>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.</p> <p><a href="http://www.ftter.com/desktopmodules/framework/api/dare/dares?%24expand=ToUser" rel="nofollow">http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=ToUser</a></p> <p>You can see the ToUserId ForeignKey in the response above, but the ToUser properties are NULL (the user definitely exists)</p> <p>You can see the ToUser EF navigation property in the metadata.</p> <p>When I use <strong>.Include</strong> on the server side I can populate it with EF, but I don't want to do this.</p> <p>I checked the Breeze Tutorial 2 here on Expand: <a href="http://learn.breezejs.com/" rel="nofollow">http://learn.breezejs.com/</a> Here is it without expand: <a href="http://learn.breezejs.com/api/northwind/Products" rel="nofollow">http://learn.breezejs.com/api/northwind/Products</a></p> <p>and here it is with Expand (And you can see the additional Category info): <a href="http://learn.breezejs.com/api/northwind/Products?%24expand=Category" rel="nofollow">http://learn.breezejs.com/api/northwind/Products?$expand=Category</a></p> <p>This is what I am trying to do but mine does not fill it...</p> <p><strong>UPDATE:</strong> I downloaded the <a href="http://www.breezejs.com/documentation/download" rel="nofollow">Breeze 1.3.6 Samples</a> and loaded the DocCode solution in VS2011. I ran it and saw that the client-side expand works; e.g. <code>http://localhost:47595/breeze/Northwind/Orders?$top=1</code> (no expand) <code>http://localhost:47595/breeze/Northwind/Orders?$top=1&amp;$expand=Customer</code> (expands customer correctly).</p> <p>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:</p> <p><strong>Breeze Sample that Works</strong></p> <pre><code>[ForeignKey("CustomerID")] [InverseProperty("Orders")] public Customer Customer {get; set;} </code></pre> <p>It just doesn't make sense... it is something to do with my WebAPI controller or EntityFramework relationship...</p> <p><strong>UPDATE 2</strong> I downloaded the most basic ToDo Knockout Breeze sample and added this line to the ToDoItem class: <code>public User ToUser { get; set; }</code> I am then able to Expand the WebAPI call <code>with http://localhost:63030/breeze/todos/Todos?$expand=ToUser</code></p> <p>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.</p> <p><strong>UPDATE 3</strong> 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).</p> <p>For example: if you look at this Expand on the Breeze site that works, <a href="http://learn.breezejs.com/api/northwind/Products?%24expand=Category" rel="nofollow">http://learn.breezejs.com/api/northwind/Products?%24expand=Category</a>, try change the last param to an invalid field and it throws an error, e.g. : <a href="http://learn.breezejs.com/api/northwind/Products?%24expand=Category1" rel="nofollow">http://learn.breezejs.com/api/northwind/Products?%24expand=Category1</a></p> <p>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: <a href="http://www.ftter.com/desktopmodules/framework/api/dare/dares?%24expand=To4657657User" rel="nofollow">http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=To4657657User</a></p> <p>Hence I am stumped.. I have no idea why this is not working.</p> <p><strong>My Code</strong></p> <pre><code> [HttpGet] [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)] public HttpResponseMessage Dares() { var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable&lt;Dare&gt;)contextProvider.Context.Dares); return ControllerUtilities.GetResponseWithCorsHeader(response); } </code></pre> <p>and here is the generated class from my EF model (using Database First)</p> <pre><code>public partial class Dare { public int DareId { get; set; } public int ToUserId { get; set; } public virtual User ToUser { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload