# EF多表关联或主从明细查询



   //EF多表关联查询,除了 dbContext.Set<Sys_Menu>()这里获取dbcontext的区别,其他与原生EF操作一样
    //注:如果需要多表关联并且需要在页面显示,请创建视图,再生成代码,如果还需要操作增加、删除、修改等,见最下面视图操作

    //其他多表关联操作,可以写原生sql,使用  DBServerProvider.SqlDapper.QueryList调用,见上面dapper操作
    List<Permissions> _permissions = (from a in dbContext.Set<Sys_Menu>()
                                        join b in dbContext.Set<Sys_RoleAuth>()
                                        on a.Menu_Id equals b.Menu_Id
                                        where b.Role_Id == roleId  && b.AuthValue != ""
                                        orderby a.ParentId
                                        select new Permissions
                                        {
                                            Menu_Id = a.Menu_Id,
                                            ParentId = a.ParentId,
                                            TableName = a.TableName,
                                            MenuAuth = a.Auth,
                                            UserAuth = b.AuthValue
                                        }).ToList();


    //主子表明细查询
     var list=  repository.FindAsIQueryable(x => true)
                .Include(x => x.明细表) //使用Include关联明细表一起查询
                .ToList();

    foreach (var item in list)
    { 
        item.明细表
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31