方式一:为实体配置默认导航并设置includeDetails属性为true
[DependsOn(typeof(AbpEntityFrameworkCoreMySQLModule))]
public class EntityFrameworkCoreModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpEntityOptions>(options =>
{
options.Entity<Order>(x =>
{
x.DefaultWithDetailsFunc = query => query.Include(o => o.OrderDetails);
});
});
}
}
//第二个参数是includeDetails,不填默认是true,若为false则不会加载
var order = await _orderRepository.FindAsync(orderId);
或者
var order = await _orderRepository.FindAsync(orderId,true);
或者
var order = await _orderRepository.GetAsync(orderId);
或者
var order = await _orderRepository.GetAsync(orderId,true);
方式二:Include
var dbContext = await GetDbContextAsync();
//方式一
//var queryable = dbContext.Set<Order>().Include(x => x.OrderDetails);
//方式二
var queryable = (await GetQueryableAsync()).Include(x => x.OrderDetails);
//方式三
var queryable = (await this.WithDetailsAsync()).Include(x => x.OrderDetails);
return await queryable.WhereIf(orderId.isNotNull(), x =>x.Id==orderId).ToListAsync();
方式三:withDetailAsync
var queryable = await _orderRepository.WithDetailsAsync(x => x.OrderDetails);
var order = queryable.Where(x => blogIds.Contains(x.Id)).FirstOrDefault();
var queryable = await WithDetailsAsync(x => x.OrderDetails);
return await queryable.WhereIf(orderId.isNotNull(), x =>x.Id==orderId).ToListAsync();
扩展1:单个实体加载多个导航属性
var queryable = await _orderRepository.WithDetailsAsync(x => x.OrderDetails1,x=>x.OrderDetails2);
var queryable = (await this.WithDetailsAsync()).Include(x => x.OrderDetails1).Include(x => x.OrderDetails2);
扩展2:单个实体加载带有子孙的导航属性
var queryable = (await this.WithDetailsAsync()).Include(x => x.OrderDetails1).ThenInclude(x => x.Goods1);
标签:VNext,orderId,await,Abp,OrderDetails,var,Include,queryable,加载
From: https://www.cnblogs.com/mingcore/p/18555313