不支持在微风中在同一查询中执行选择和扩展 [英] Perform a select and expand in the same query with breeze is not supported

查看:43
本文介绍了不支持在微风中在同一查询中执行选择和扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Durandal / breeze开发了一个asp.net解决方案。



这里是我让所有托运人使用的代码:

  var query = EntityQuery.from('Shippers')
.select('id,name,street,city');

return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);

以下是相关模型:

 公共类托运人
{
[关键字]
public int Id {get;组; }
公共字符串Name {get;组; }
公共字符串Street {get;组; }
公用字串Number {get;组; }
public City City {get;组; }
}

公共类城市
{
public int Id {get;组; }
公共字符串Name {get;组; }
公用字串PostCode {get;组; }
public Country国家{组; }
}

现在我还需要包括国家/地区

 公共类国家
{
[关键字]
public int Id {get;组; }
公共字符串代码{get;组; }
公共字符串Name {get;组; }
}

但是在实际查询中,我没有国家/地区。 / p>

我尝试:

  var query = EntityQuery.from('Shippers ')
.select('id,名称,街道,城市')
.expand('City.Country');

但是我得到了错误:



目前不支持在同一查询中同时使用'expand'和'select'



我的问题:如何获得国家/地区?






更新



正如Jay所建议的,我们可以这样做:

  var query = EntityQuery.from('托运人')
.select('id,名称,街道,城市,城市。国家')

现在,我得到一个 city_Country 对象:





我不明白为什么要得到这个 city_Country ,因为国家/地区数据已在城市\国家/地区对象中提供:





此外,它还给我带来了问题,因为我的下一条语句试图将dto映射到到我的实体,此 city_Country 对象在我的实体中不存在,并且在映射时发生了错误。



下面我们看到我的实体对象,没有 city_Country 对象:





我是否必须对映射做一些特殊的操作来避免它?



下面是我进行映射操作的函数:

  function mapToEntity(实体,dto){
//实体是具有可观察对象的对象
// dto来自于json
,用于(dto中的var prop){
if(dto.hasOwnProperty(prop )){
实体[prop](dto [prop]);
}
}
个返回实体;
}


解决方案

我不确定使用投影来部分填充实体的优良作法,这就是您正在做的事情。 Breeze会自动映射在结果集中找到的所有真实实体。那么为什么不简单地使用

  var query = EntityQuery.from('Shippers')
.where(... )
.expand('city.country');

直接结果将是托运人的集合,但每个托运人将拥有其城市和嵌套国家的财产也完全作为实体解决。它们可以通过返回的发件人进行导航,但是如果您想直接查询它们,它们也可以在EntityManager缓存中使用。



第二个注意事项:Breeze的扩展语义与实体框架具有相同的限制。这意味着我们无法扩展投影的属性。



所以您可以跳过投影(如上所述)

  var query = EntityQuery.from('Shippers')
.where(...)
.expand('city.country')

,然后获得具有 city和 country属性的完整 Shipper实体,或者在其中填充...。或者您可以进行投影,如果您自己执行等价的扩展,即



在这种情况下,您将

  var query = EntityQuery.from('Shippers')
.select('id,name,street,city,city.country')

在这种情况下,结果集中的每个项目都将包含5个属性,请注意,在这种情况下,只有'city'和'city.country'属性是因为这些是结果集中唯一的真实实体,所以添加到了entityManager的缓存中,即没有托运人的实体。



要明确的想法是查询和酷儿的副作用 y是不同的。查询的顶级结果将完全符合您的期望形状。查询的副作用是您执行的任何扩展的结果。它们不会改变查询的形状,它们只是改变结果中任何嵌套实体属性的分辨率。



希望这会有所帮助。


I develop an asp.net solution with Durandal/breeze.

Here is my code to get all my shippers:

var query = EntityQuery.from('Shippers')
               .select('id, name, street, city');

return manager.executeQuery(query)
        .then(querySucceeded)
        .fail(queryFailed);

Here is the related model:

public class Shipper
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Street { get; set; }
    public string Number { get; set; }
    public City City { get; set; }
}

public class City
{
    public int Id { get; set; }        
    public string Name { get; set; }
    public string PostCode { get; set; }
    public Country Country { get; set; }
}

Now I need to also include countries

public class Country
{
    [Key]
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

But with the actual query I don't get countries with it.

I try:

var query = EntityQuery.from('Shippers')
               .select('id, name, street, city')
               .expand('City.Country');

but i get the error:

use of both 'expand' and 'select' in the same query is not currently supported

My question: how to get countries?


UPDATE

As Jay suggested, we can do:

var query = EntityQuery.from('Shippers')
       .select('id, name, street, city, city.country')

Now, I got a city_Country object:

I don't understand why we got this city_Country because country data are already available in the city \ country object:

Furthermore it gives me problem because my next statement try to map my dto into my entity and this city_Country object don't exist in my entity and an error occured when mapping.

Below we see my entity object and there are no city_Country object:

Do I have to do something special on my mapping to avoid it?

Below is my function for the mapping operation:

function mapToEntity(entity, dto) {
     // entity is an object with observables
     // dto is from json
     for (var prop in dto) {
          if (dto.hasOwnProperty(prop)) {
             entity[prop](dto[prop]);
          }
     }
     return entity;
}

解决方案

I'm not sure it's good practice to use a projection to 'partially' fill an entity, which is what it looks like you are doing. Breeze will automatically map any true 'entities' it finds in your result set. So why not simply use

var query = EntityQuery.from('Shippers')
    .where(...)
    .expand('city.country');

The immediate results will be a collection of shippers, but each shipper will have its city and nested country properties fully resolved as entities as well. They will be available via navigation from the returned shippers but they will also be available within the entityManager cache if you wanted to query them directly.

A second note: Breeze 'expand' semantics have the same restrictions that the Entity Framework does. This means that we cannot expand the properties of a projection.

So you can either skip the projection (as above)

var query = EntityQuery.from('Shippers')
   .where(...)
   .expand('city.country')

and get full "Shipper" entities with the 'city" and the 'country' property on the city both populated. ... Or you can do a projection, in which case you perform the equivalant of the expansion yourself. i.e.

In this case you will

var query = EntityQuery.from('Shippers')
  .select('id, name, street, city, city.country')

in which case each item in your result set will consist of 5 properties. Note that in this case only the 'city' and 'city.country' properties will be added to the entityManager's cache becasue these are the only 'true' entities in the result set. i.e. no shipper's

The idea to be clear on is that the 'results' of a query and the 'side effects' of a query are distinct. The top level results of a query will be exactly the shape you expect. The 'side effects' of the query are the result of any 'expand' you perform. These do not change the shape of the query, they simply change the resolution of any nested 'entity' properties within the results.

Hope this helps.

这篇关于不支持在微风中在同一查询中执行选择和扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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