属性 不是实体类型的导航 属性

The property is not a navigation property of entity type

我设计了下图中的实体。

对于此模式,我会在 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; }
    }

public class 用户角色 {

    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; }

    }

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

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

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