Skip to content
This repository has been archived by the owner on Apr 19, 2021. It is now read-only.

如何在框架中关闭Entityframework的延迟加载,而改用显式加载? #18

Open
wuyou331 opened this issue May 12, 2015 · 1 comment

Comments

@wuyou331
Copy link

目前来看需要在Repository类的构造函数中添加以下代码
((DbContext)unitOfWork).Configuration.LazyLoadingEnabled = false;

是否还有更合理的做法?

@gmf520
Copy link
Owner

gmf520 commented May 12, 2015

1.LazyLoadingEnabled = false只是禁用延迟加载,当调用entity.Property的时候,返回null
2.不大理解你说的显示加载是什么操作,是使用Entry获取实体信息之后,再使用Collection或Reference方法获取和操作实体关联的导航属性吗,这个操作和使用延迟加载执行的语句是一样的,没多大区别
3.如果要提前加载导航属性,减少延迟加载带来的查询次数,可以使用Include来进行贪婪加载

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants