我有一个机架模型和一个服务器模型。目前,当我想检索单个服务器时,我执行以下操作,以指定显式加载:-
public Server FindAllServer(int id)
{
return project.Servers.Where(c => c.ServerID == id)
.Include(a => a.OperatingSystem)
.Include(a2 => a2.DataCenter)
.Include(a3=>a3.Rack)
.Include(a4=>a4.ServerModel)
.Include(a5=>a5.Technology)
.Include(a6=>a6.VirtualMachines)
.Include(a7=>a7.TechnologyStatu)
.Include(a8=>a8.TechnologyBackUpStatu)
.Include(a9=>a9.TechnologyRole)
.SingleOrDefault();
}
但是现在我想显示一个机架及其所有服务器,我做了以下事情:-
public Rack FindAllRack(int id)
{
return project.Racks.Where(c => c.RackID == id)
.Include(a => a.Servers)
.Include(a2 => a2.DataCenter)
.Include(a3 => a3.Firewalls)
.Include(a4 => a4.Routers)
.Include(a5 => a5.Technology)
.Include(a6 => a6.StorageDevices)
.Include(a7=>a7.Switches)
.Include(a8=>a8.Zone)
.SingleOrDefault();
}
所以我不确定如何定义以明确包含机架下服务器的所有导航属性,因为我无法编写以下内容:-
project.Racks.Where(c => c.RackID == id).Include(a => a.Servers.Include(………))
问题是,如果我在一个机架下有 50 台服务器,那么对于每台服务器,将有大约 7 个请求来检索服务器导航属性,所以我将有大约 350 个请求!!!