【问题标题】:Entity Framework Querying with Navigate Objects使用导航对象进行实体框架查询
【发布时间】:2012-03-06 12:51:19
【问题描述】:

我下面有实体类,我想要一个像 sql 这样的查询,实体框架怎么可能??

select * from SiteUsers su
inner join SiteUserRoles sur on su.Id=sur.SiteUserId
inner join SiteRoleActions sra on sur.SiteRoleId = sra.SiteRoleId
inner join SiteActions sa on sa.Id = sra.SiteActionId
where su.Id=1 and sa.ParentId=189



  public class SiteRole
    {
        public SiteRole()
        {
            this.SiteActions = new HashSet<SiteAction>();
            this.SiteUser = new HashSet<SiteUser>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public virtual ICollection<SiteAction> SiteActions { get; set; }
        public virtual ICollection<SiteUser> SiteUser { get; set; }
    }

 public partial class SiteUser
    {
        public SiteUser()
        {
            this.SiteRoles = new HashSet<SiteRole>();
        }

        public int Id { get; set; }
        public string Email { get; set; }
        public string UserPassword { get; set; }
        public string UserName { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public Nullable<System.DateTime> DateCreated { get; set; }

        public virtual ICollection<SiteRole> SiteRoles { get; set; }
    }

 public partial class SiteAction
    {
        public SiteAction()
        {

            this.Childs = new HashSet<SiteAction>();
            this.SiteRoles = new HashSet<SiteRole>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string ImagePath { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
        public Nullable<int> ParentId { get; set; }
        public Nullable<int> Type { get; set; }


        public virtual ICollection<SiteAction> Childs { get; set; }
        public virtual SiteAction Parent { get; set; }
        public virtual ICollection<SiteRole> SiteRoles { get; set; }
    }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SiteUser>().HasMany<SiteRole>(r => r.SiteRoles).WithMany(u => u.SiteUser).Map(m =>
        {
            m.ToTable("SiteUserRoles");
            m.MapLeftKey("SiteUserId");
            m.MapRightKey("SiteRoleId");
        });

        modelBuilder.Entity<SiteRole>().HasMany<SiteAction>(r => r.SiteActions).WithMany(u => u.SiteRoles).Map(m =>
        {
            m.ToTable("SiteRoleActions");
            m.MapLeftKey("SiteRoleId");
            m.MapRightKey("SiteActionId");
        });



        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    }

【问题讨论】:

    标签: c# .net linq entity-framework entity-framework-4


    【解决方案1】:

    由于您没有 SiteUserRoles 和 SiteRoleActions 作为实体,而只是作为连接表(如果我没看错的话),您应该使用导航属性而不是连接。比如:

    conext.SiteUsers.Where(su => su.id == 1
        && su.SiteRoles.SelectMany(sr => sr.SiteActions)
            .Any(sa => sa.ParentId == 189 )
    

    (未检查语法)。

    【讨论】:

      【解决方案2】:
        var result=   (from su in SiteUsers
                 join sur in SiteUserRoles
                 on su.Id=sur.SiteUserId
                 join sra in SiteRoleActions
                 on sur.SiteRoleId = sra.SiteRoleId
                 join sa in SiteActions
                 on sra.SiteActionId=sa.Id
                 where su.Id=1 and sa.ParentId=189
                 select su).ToList();
      

      希望这会有所帮助。结果将是 SiteUsers 类型的列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-31
        相关资源
        最近更新 更多