微风不扩展导航属性 [英] Breeze does not expand a navigation property

查看:82
本文介绍了微风不扩展导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单页应用程序中有一个Breeze数据服务(又名datacontext).我想从WebAPI控制器获取运行列表以及每次运行的OutlineItems列表.

I have a Breeze dataservice (aka datacontext) in my Single Page Application. I want to get a list of Runs from a WebAPI controller along with a list of OutlineItems for each run.

控制器通过BreezeController上的此方法返回带有子OutlineItems的打开运行的列表.

The controller is returning the list of open Runs with child OutlineItems with this method on the BreezeController.

[AcceptVerbs("GET")]
public IQueryable<Run> Runs()
{
return _contextProvider.Context.Runs
    .Include("RunOutlineItems")
    .AsQueryable()
    .Where(r => r.RunStatusId < 4);    // 4 is the cutoff for open items             

}

这是数据模型.

namespace PilotPlantBreeze
{
    public class Run
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Rundate { get; set; }
        public DateTime? RunStart { get; set; }
        public int MachineId { get; set; }
        public int ProductId { get; set; }
        public int RunStatusId { get; set; }
        public string Comments { get; set; }

        // Nav props
        public IList<OutlineItem> RunOutlineItems { get; set; }
    }
}       

当我查看来自WebAPI请求的Response数据时,RunOutlineItems的列表位于JSON中.例如,这是一项:

When I look at the Response data from the WebAPI request, the list of RunOutlineItems is in the JSON. Here is one item for example:

{
    "$id":"27",
    "$type":"PilotPlantBreeze.OutlineItem,PilotPlantBreeze.Model",
    "Id":22,
    "RunId":5,
    "TankId":4,
    "OutlineTopicId":1,
    "OutlineDescriptionId":9,
    "PersonId":1,
    "Value":"23"
}

这是我的客户端javascript代码,用于从WebAPI获取数据. 为了清楚起见,省略了错误检查和本地缓存检查.

Here is my client side javascript code to get the data from the WebAPI. Error checking and local cache checking omitted for clarity.

var getRuns = function () {
    // The EntityQuery is defined at the beginning of the dataservice
    //  Here I am asking for a query on the server.  Note the .expand 
    //  which is supposed to avoid lazy loading.  Lazy loading is turned off 
    //  on the WebAPI already.
    var query = EntityQuery
    .from("Runs")
    .expand("RunOutlineItems")
    .orderBy("rundate");

    // The manager is defined at the beginning of the dataservice
    //  Here I am asking the manager to execute the query with a promise
    return manager.executeQuery(query)
    .then(runsQuerySucceeded)
    .fail(runsQueryFailed);

    // The promise does not fail, but I would put an error in here if it ever does
    function runsQueryFailed(data) {
    }
    // When the promise succeeds, the data parameter is the JSON from the WebAPI
    function runsQuerySucceeded(data) {
        //
        // When I stop the debugger here data.results holds the entities, but
        //  the child entities for the RunOutlineItems is an observableArray with 
        //  nothing in it.
        //
        app.vm.runs.runs(data.results);
    }
};

所以我的问题是如何将子项放入我的viewModel中.我有一种解决方法,可以在服务器上对WebAPI的单独调用中获取子项,并使用自定义ko.bindHandler对其进行处理,但是使用导航技术会很方便.

So my question is how to get the child items into my viewModel. I have a workaround which gets the child Items in a separate call to the WebAPI on the server and processes them with a custom ko.bindHandler, but it would be convenient to have the navigation technology working.

推荐答案

您既不需要"include"(服务器端)又不需要expand(客户端);任何人都应该做到这一点.

You shouldn't need both the 'include' (server side) and the expand (client side); either one should do the trick.

因此,我不理会客户端查询,而是将服务器端查询修改为:

So I'd leave you client side query alone and modify the server side query to just this:

[AcceptVerbs("GET")]
public IQueryable<Run> Runs() {
    return _contextProvider.Context.Runs
      .Where(r => r.RunStatusId < 4);    // 4 is the cutoff for open items             

}

请注意,AsQueryable()也消失了.

Note that the AsQueryable() is gone as well.

如果这不起作用,请发回.

If this doesn't work, please post back.

这篇关于微风不扩展导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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