选择单独的导航属性时,选择嵌套的导航属性将生成相同的SQL语句

时间:2013-07-11 23:48:03

标签: asp.net-mvc entity-framework-4.1

我有以下存储库方法: -

public AccountDefinition GetCustomer2(int id)
        {      var c = entities.AccountDefinitions.Where(p => p.ORG_ID == id)
               .Include(a => a.SDOrganization)
               .Include(a2 => a2.SiteDefinitions)
                .Include(a3 => a3.SDOrganization.AaaPostalAddresses)
                .Include(a4 => a4.SiteDefinitions.Select(a5 => a5.DepartmentDefinitions.Select(a6 => a6.SDUsers.Select(a7 => a7.AaaUser.AaaContactInfoes)))).SingleOrDefault();
return c; }

但是当我评论一些代码时,我发现调用Action方法时生成的SQL语句将是相同的: -

public AccountDefinition GetCustomer2(int id)
        {      var c = entities.AccountDefinitions.Where(p => p.ORG_ID == id)
           //    .Include(a => a.SDOrganization)
            //   .Include(a2 => a2.SiteDefinitions)
                .Include(a3 => a3.SDOrganization.AaaPostalAddresses)
                .Include(a4 => a4.SiteDefinitions.Select(a5 => a5.DepartmentDefinitions.Select(a6 => a6.SDUsers.Select(a7 => a7.AaaUser.AaaContactInfoes)))).SingleOrDefault();
return c; }

这是否意味着当我导航到嵌套的Navigation属性时,EF会自动检索父导航属性吗? 。例如,当我写.Include(a3 => a3.SDOrganization.AaaPostalAddresses)时,则无需编写.Include(a => a.SDOrganization)

1 个答案:

答案 0 :(得分:1)

简而言之,是的,否则你无法穿越那个导航属性

EG,访问AccountDefinition.SDOrganization.AaaPostalAddresses的唯一方法是SDOrganization不为空。

话虽如此,我个人的偏好是通过列出它来明确表达这个中间包含。虽然这没有任何功能上的好处,但它会提醒您此属性也将被返回