1

我有一个机架模型和一个服务器模型。目前,当我想检索单个服务器时,我执行以下操作,以指定显式加载:-

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 个请求!!!

4

3 回答 3

1

我认为您应该能够Include()通过指定属性路径来使用基于字符串的重载来做到这一点,例如

project.Racks.Where(c => c.RackID == id).Include("Servers.SomePropertyOfServer")

编辑:在做了更多研究之后,看来这也可能有效:

project.Racks.Where(c => c.RackID == id).Include(a => a.Servers.Select(s => s.SomePropertyOfServer))

请参阅此链接,特别是标题为“Eagerly Loading Multiple Levels”的部分以获取示例。

于 2013-07-24T14:24:37.170 回答
1

If all you need is the count of the various child objects, consider the following instead. It should issue a single request to the database only fetching up to 2 rows. Profile the request to make sure this is the case as I haven't tested it.

        return project.Racks.Where(c => c.RackID == id)
            .Select(a => new {
                serverCount = a.Servers.Count(),
                dataCenterCount = a.DataCenter.Count(),
                firewallCount = a.Firewalls.Count(),
                routerCount = a.Routers.Count(),
                technologyCount = a.Technology.Count(),
                storageDeviceCount = a.StorageDevices.Count(),
                switchCount = a.Switches.Count(),
                zoneCount = a.Zone.Count()
            }).SingleOrDefault();

Also, if you aren't planning on throwning an exception if you get more than one record, use FirstOrDefault instead of SingleOrDefault.

于 2013-07-25T13:16:46.317 回答
0

你试过这个吗?

return project.Servers
              .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)
              .Where(srv => srv.Rack.RackID == id);
于 2013-07-24T14:23:28.797 回答