通过使用EF Core 2.1的.Net Core Web API应用程序工作,我试图防止在使用include时加载父级的所有子级记录。
设置如下: 该API正在提取IEnumerable客户集合。我有一个Include语句来提取来自单独表的CustomerType。
[HttpGet]
public IEnumerable<Customer> GetCustomer()
{
IEnumerable<Customer> customerList = _context.Customer
.Include(i => i.CustomerType);
return customerList;
}
当返回IEnumerable集合时,每个客户都包含CustomerType对象,然后该对象还包括具有该CustomerType的所有客户的Customer集合。您可以想象,这正在创建巨大的数据集。
如何防止EF Core包含CustomerType的Customer集合?我尝试使用
关闭延迟加载_context.ChangeTracker.LazyLoadingEnabled = false;
但是没有效果。
答案 0 :(得分:0)
根据vivek的回复,我已经成功更新了API,如下所示:
[HttpGet]
public object GetCustomer()
{
var customerList = _context.Customer
.Select(s => new
{
s,
s.CustomerType.CustomerType
});
return customerList;
}