【问题标题】:Breeze **expand** vs. EntityFramework **include** for eager loading of navigation propertiesBreeze **expand** vs. EntityFramework **include** 用于快速加载导航属性
【发布时间】:2017-05-04 11:09:29
【问题描述】:

a) 为了在 server 端使用 EntityFramework 加载实体的导航属性,我可以使用 include

public virtual IQueryable<TEntity> All(){
  IQueryable<TEntity> initialQuery = Context.Set<TEntity>();
  IQueryable<TEntity> extendedQuery = initialQuery.Include('MyNavigationProperty');
  return extendedQuery;
}

另见https://msdn.microsoft.com/en-us/magazine/hh205756.aspx

b) Breeze 允许使用 expandclient 端加载导航属性:

var navQuery = breeze.EntityQuery.from('MyEntity')  
    .expand('MyNavigationProperty');

另见https://breeze.github.io/doc-js/navigation-properties.html

=> 如果我想加载导航属性,我应该同时使用这两个选项吗?如果不是,那么在服务器端或客户端定义急切加载的导航属性的优缺点是什么?选择其中一个选项时,是否需要考虑性能或安全问题?

例如,是否可以破解客户端代码以检索比原始代码加载的更多导航属性?

这里有人说使用 include 或 expand 就足够了:

Breeze does not expand a navigation property

但是,我仍然不确定如何/何时使用它们。

【问题讨论】:

    标签: javascript c# entity-framework breeze navigation-properties


    【解决方案1】:

    似乎是这样的(如果需要,请纠正我或添加更多信息):

    a) Breeze .expand 选项默认启用。为了禁用或限制它,可以在域控制器中应用注释 EnableBreezeQuery

    [HttpGet]
    [AllowAnonymous]
    [EnableBreezeQuery(MaxExpansionDepth = 0)]
    public IQueryable<Network> NetworkForEntryPageBy()
    {
      return _unitOfWork.NetworkRepository.All();
    }
    

    这将禁止客户端使用微风扩展。

    另见

    https://github.com/Breeze/breeze.server.net/issues/12

    https://github.com/Breeze/breeze.server.net/blob/master/Tests/Test.WebApi2.EF6/Controllers/NorthwindIBModelController.cs

    https://github.com/IdeaBlade/Breeze/pull/35

    b) 如果启用了微风扩展,它可以用于覆盖服务器端包含。例如,我在服务器端包含了“图片”导航属性,并在客户端扩展了“公司”导航属性。我首先期望两个导航属性都可用。但是,客户端仅填写“公司”列表:


        [HttpGet]
        [AllowAnonymous]
        [EnableBreezeQuery(MaxExpansionDepth = 10)]
        public IQueryable<Network> NetworkForEntryPageBy()
        {
          return _unitOfWork.NetworkRepository.All().Include('Pictures');
        }
    

    self.networksForEntryPage = function () {
            var query = breeze.EntityQuery
              .from(routeconfig.networksForEntryPageUrl).expand('Companies');
            return self.executeQuery(query);
          };
    

    => 图片为空 => 公司不为空,可供客户使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      相关资源
      最近更新 更多