【问题标题】:The property is not a navigation property of entity type该属性不是实体类型的导航属性
【发布时间】:2017-12-29 04:26:53
【问题描述】:

我已经设计了我的实体,附在下图中。

对于这个架构,我会在 sql 中编写以下查询,以通过以下方式获取该用户的所有角色、活动、应用程序

select * from users u, roles r, userroles ur, roleappactivities raa, applications ap, activities ac
where u.Id = ur.UserId
and ur.RoleId = r.Id
and r.Id = raa.RoleId
and raa.ApplicationId = ap.Id
and raa.ActivityId = ac.Id
and u.id = 1

为了在我的核心应用程序中实现同样的目标,我编写了以下代码,但失败了。我对如何通过以下代码实现上述查询没有任何想法。非常感谢任何帮助。

_context.Users
                .Include("Roles")
                .Include("RoleAppActivities")
                .Include("Applications")
                .Include("Activities")
                .Where(x => x.Id == id)
                .Select(x => new User
                {
                    Id = x.Id,
                    TId = x.TId,
                    Roles = x.Roles

                })

编辑:

这是我的实体

 public class User
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }        
        public ICollection<UserRole> Roles { get; set; }
    }

公共类 UserRole {

    public int UserId { get; set; }
    public User User{ get; set; }

    public int RoleId { get; set; }
    public Role Role{get; set;}
}
 public class Role
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public ICollection<UserRole> Users { get; set; }
        public ICollection<RoleApplicationActivity> RoleApplicationActivity { get; set; }
    }

 public class RoleApplicationActivity
    {

        public int Id { get; set; }
        public int RoleId { get; set; }
        public int ApplicationId { get; set; }
        public int ActivityId { get; set; }

        public Activity Activity { get; set; }
        public Application Application { get; set; }
        public Role Role { get; set; }
    }

public class Activity
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<RoleApplicationActivity> RoleApplicationActivity { get; set; }
    }


 public class Application
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<RoleApplicationActivity> RoleApplicationActivity { get; set; }

    }

【问题讨论】:

标签: linq ef-core-2.0


【解决方案1】:

我认为您的实体 User 应该拥有其他集合(角色, 角色应用活动..)。所以你可以使用 .include(user=> user.Collection) 直接加载它们,我认为它比使用 .include("string)"..

【讨论】:

    【解决方案2】:

    我必须将我的查询修改为以下以解决问题。

    from u in _context.Users
                       from r in _context.Roles
                       from app in _context.Applications
                       from ac in _context.Activities
                       where u.Id == 1
                       select u
    

    【讨论】:

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