一、EF Core 查询出关联表数据
EF Core 默认不支持查询中带出关联表数据,如果需要则使用Include() 声明加载
ctx.Categories
.Include(c => c.Products)
.Include(c => c.News);
二、EF Core 关联表条件筛选
场景案例举例子
1. Sql 语句场景 需求举例
---查询指定角色下的菜单
select * from Sys_Navigation
where NavID in(
select NavID from Sys_Role_Nav where RoleID=1
)
---查询拥有指定菜单的角色
select * from Sys_Role
where RoleID in (
select RoleID from Sys_Role_Nav where NavID=1
)
---查询拥有指定菜单的用户
select * from Sys_Employee where RoleID in (
select RoleID from Sys_Role_Nav where NavID=1
)
2.EF core 对应场景举例 整理
static void Test1()
{
//查询指定角色下的菜单 会员角色的菜单
//查找菜单,从关联表中,含有roleid=1的菜单
//roleid=1
QLSingleContext _context = new QLSingleContext();
var navList = _context.SysNavigations.Where(q => q.SysRoleNavs.Any(i => i.RoleId == 1))
.ToList();
foreach (var item in navList)
{
Console.WriteLine($"id:{item.NavId} name:{item.Name}");
}
}
更多:
EF Linq To Sql 常用查询整理_常用使用方式整理
EF null字段排序后置_Sql中Null字段排在后边
更多推荐