abp.io如何在存储库中获得2个或更多的详细信息?



我有实体:

class Contract
{
public TenantProfile TenantProfile { get; set; }
public ContractStatus Status { get; set; }
}

Service (override GetAsync(Id)):

var contractWithDetails = (await Repository.WithDetailsAsync(x => x.Status)).FirstOrDefault(x => x.Id == id);

但是属性TenantProfile- null,因为我不能对IQueryable执行WithDetailsAsync。如何解决我的问题并执行多于2个WithDetailsAsync?

建议为每个聚合根创建一个扩展方法with子集合:

public static IQueryable<Contract> IncludeDetails(
this IQueryable<Contract> queryable,
bool include = true)
{
if (!include)
{
return queryable;
}
return queryable
.Include(x => x.TenantProfile)
.Include(x => x.ContractStatus);
}

现在你可以重写WithDetailsAsync:

public override async Task<IQueryable<Contract>> WithDetailsAsync()
{
// Uses the extension method defined above
return (await GetQueryableAsync()).IncludeDetails();
}

现在你的WithDetailsAsync方法包括这两个。

查看更多ABP实体框架核心集成最佳实践文档。

最新更新