ASP.NET MVC + EF的多表链接查询问题

EF中遇到多表查询问题,这两张表属于两个不同的数据库,这两张表事实上是有外键的,但是只是数据库中没有设外键,所以就不能像标准EF做法,直接取外键。

  比如A数据库中 T_Students (SID,Name,Sex,Age,...) 和 B数据库中T_StuScore(SCID,SID,Score,...)

  IQueryable<T_Students> students = dbContext1.T_Students;

  IQueryable<T_StuScore> stuScore = dbContext2.T_StuScore;

 var result = students.Join(stuScore, a => a.SID, b => b.SID, (a, b) => new{SID=a.SID,Score=b.Score,...})

或者用linq toentity

result = from a in students
            join b in stuScore on a.SID equals b.SID

            select new{a.SID,b.Score,...}

这样是报错的,The specified LINQ expression contains references to queries that are associated with different contexts.因为一个linq语句中是不能有两个不同的dbcontext的所以我的做法是,我只能将一个先转化为list

  IQueryable<T_Students> students = dbContext1.T_Students;

  List<T_StuScore> stuScore = dbContext2.T_StuScore.ToList();

var result = students.Join(stuScore, a => a.SID, b => b.SID, (a, b) => new{SID=a.SID,Score=b.Score,...})

这样可以查询出来,但是我的T_Students有7~8万学生数据时,这样任何一个先转化为list都会影响EF的延迟加载,影响查询效率。

请高手指点,怎么在ASP.NET MVC +EF下多表查询实现比较高的效率??

PS:我正在尝试用MIN,MAX原理的主键分页方案,用EF调用分页存储过程,实现高效分页查询。

.net技术asp.netasp.net mvcentity frameworkc#

问题补充:

正在参照这种方式,以前在SQL查询的方式做过,存储过程分页,效率还是比较客观的哦。

http://www.cnblogs.com/kezhiping/p/3878049.html?utm_source=tuicool&utm_medium=referral

但是这样还是旧的SQL的思想,只是与EF强制结合起来,并没有真正发挥EF的优势。

猜你喜欢

转载自blog.csdn.net/qmdweb/article/details/82182004
今日推荐