EntityFramework代码优先模式里面的多对多查询
菜单类
public class BaseMenu
{
[Key]
public int Id { get; set; }
[Display(Name = "名称")]
[Required(ErrorMessage = "不能为空!")]
[MaxLength(50, ErrorMessage = "长度不能超过50个字符!")]
public string Name { get; set; }
public virtual ICollection<BaseRole> Roles { get; set; }
}
public class BaseRole
{
[Key]
public int Id { get; set; }
[Display(Name = "名称")]
[Required(ErrorMessage = "不能为空!")]
[MaxLength(50, ErrorMessage = "长度不能超过50个字符!")]
public string Name { get; set; }
public virtual ICollection<BaseUser> Users { get; set; }
public virtual ICollection<BaseMenu> Menus { get; set; }
}
public class BaseUser
{
[Key]
public int Id { get; set; }
[Display(Name = "用户名")]
[Required(ErrorMessage = "用户名不能为空!")]
[MaxLength(50, ErrorMessage = "长度不能超过50个字符!")]
public string UserName { get; set; }
public virtual ICollection<BaseRole> Roles { get; set; }
}
var query=from bm in db.BaseMenus
join brm in db.BaseRoleMenu on bm.Id equals brm.MenuId
join br in db.BaseRole on brm.RoleId equals br.Id
join bur in db.BaseUserRoles on br.Id equals bur.RoleId
join bu in db.BaseUsers on bur.UserId equals bu.Id
where bu.Id==206
select bm;