【问题标题】:Entity Framework navigating using navigation properties more than one level实体框架使用多于一级的导航属性进行导航
【发布时间】:2013-04-17 10:00:25
【问题描述】:

我有如下实体模型类

public partial class User
{
    public User()
    {
        this.webpages_Roles = new HashSet<webpages_Roles>();
    }

    public int UserID { get; set; }

    public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}

.

public partial class webpages_Roles
{
    public webpages_Roles()
    {
        this.Users = new HashSet<User>();
        this.Roles_X_ApplicationModules = 
                       new HashSet<Roles_X_ApplicationModules>();
    }

    public int RoleId { get; set; }


    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Roles_X_ApplicationModules> 
                                   Roles_X_ApplicationModules { get; set; }
}

.

public partial class Roles_X_ApplicationModules
{
    public long ID { get; set; }
    public Nullable<int> ModuleID { get; set; }
    public Nullable<int> RoleID { get; set; }
    public Nullable<bool> ViewPermission { get; set; }

    public virtual ApplicationModule ApplicationModule { get; set; }
    public virtual webpages_Roles webpages_Roles { get; set; }
}

.和

public partial class ApplicationModule
{
    public ApplicationModule()
    {
        this.Roles_X_ApplicationModules = 
                           new HashSet<Roles_X_ApplicationModules>();
    }

    public int ModuleID { get; set; }

    public virtual ICollection<Roles_X_ApplicationModules> 
                                      Roles_X_ApplicationModules { get; set; }
}

您可以看到User 对象具有指向webpages_Roles 的导航属性,该属性又具有指向Roles_X_ApplicationModules 的导航属性,然后又导航到ApplicationModule..

现在我想从用户那里获取所有的 ApplicationModule..如何使用导航属性编写查询..

我试过这样的东西..

var appModules = user.webpages_Roles.SingleOrDefault()
       .Roles_X_ApplicationModules.Where(z => z.ViewPermission == true)
       .Select(x => x.ApplicationModule);

但问题是,它不会向数据库发出单个查询。它拆分查询以在SingleOrDefault 处获取webpages_Roles,然后另一个查询根据RoleId 获取Roles_X_ApplicationModules,最后与Roles_X_ApplicationModules 匹配的查询数量与条件匹配以获取ApplicationModule

如何编写 LINQ 查询以便向数据库发出单个 sql 查询?

【问题讨论】:

    标签: c# .net entity-framework ef-code-first


    【解决方案1】:

    您可以使用Include() 来执行此操作。 示例:

    card = Cards.Include(l => l.DocumentLinks)
                .Include(l => l.Charges.Select(ch => ch.DocumentLinks)
                .SingleOrDefault(c=>c.Id==id);
    

    这适用于三个链接的实体:

    public class Card
    {
        public Guid Id{get;set;}
        public virtual ICollection<DocumentLink> DocumentLinks{get;set;}
        public virtual ICollection<Charge> Charges{get;set;}
    }
    
    public class Charge
    {
        ...
        public virtual ICollection<DocumentLink> DocumentLinks{get;set;}
    }
    
    public class DocumentLink
    {
        ...
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      var appModules = from u in user
                       from w in u.webpages_Roles
                       from am in w.Roles_X_ApplicationModules
                       where am.ViewPermission == true
                       select am;
      

      如果你想要预先加载,那么你只需要调用 ToList:

      var appModules = (from u in user
                       from w in u.webpages_Roles
                       from am in w.Roles_X_ApplicationModules
                       where am.ViewPermission == true
                       select am).ToList();
      

      【讨论】:

      • 您的查询发出的 sql 查询比我的方法执行的要多.. :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2016-05-29
      相关资源
      最近更新 更多