【ABP踩坑记】仓储 WithDetailsAsync 配置

使用Abp封装的CrudAppService中的 GetAsync 获取聚合根中Details为null。

 

查看源码(EfCoreRepository.cs)后发现仓储方法是有WithDetailsAsync和WithDetails方法

[Obsolete("Use WithDetailsAsync")]
public override IQueryable<TEntity> WithDetails()
{
  if (AbpEntityOptions.DefaultWithDetailsFunc == null)
  {
    return base.WithDetails();
  }

  return AbpEntityOptions.DefaultWithDetailsFunc(GetQueryable());
}

public override async Task<IQueryable<TEntity>> WithDetailsAsync()
{
  if (AbpEntityOptions.DefaultWithDetailsFunc == null)
  {
    return await base.WithDetailsAsync();
  }

  return AbpEntityOptions.DefaultWithDetailsFunc(await GetQueryableAsync());
}

 

从源码中看出有个配置项 AbpEntityOptions.DefaultWithDetailsFunc

 配置方法:

EntityFrameworkCoreModule.cs

public override void ConfigureServices(ServiceConfigurationContext context)
{
  context.Services.AddAbpDbContext<ERPDbContext>(options =>
  {
    options.AddDefaultRepositories(includeAllEntities: true);
  });

  Configure<AbpDbContextOptions>(options =>
  {
    options.UseSqlServer();
  });

  Configure<AbpEntityOptions>(options =>
  {
    options.Entity<DemandOrder>(DemandOrderOptions =>
    {
      DemandOrderOptions.DefaultWithDetailsFunc = query => query.Include(o => o.Items);
    });
  });
}    

 

上一篇:浅入ABP(1):搭建基础结构的 ABP 解决方案


下一篇:asp.net abp模块化开发之通用树2:设计思路及源码解析