Entity Framework 中把 LazyLoadingEnabled 设置为了 false,为何仍然是懒加载模式? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Dic4000
V2EX    .NET

Entity Framework 中把 LazyLoadingEnabled 设置为了 false,为何仍然是懒加载模式?

  •  
  •   Dic4000 2016-09-08 18:27:13 +08:00 3976 次点击
    这是一个创建于 3324 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 EF6.0 版本中,我把 LazyLoadingEnabled 设置为了 false ,但在查询一个具有导航属性的实体时,这个查询出来的实体其导航属性仍然为空,不知道为何会这样的行为?

    我想实现“饥渴加载”模式,在查询的时候不想显示调用 Include 、 Reference 以及 Collection 方法,就可以把实体的相关导航属性全部一次性加载进来,应该如何做呢? 主要代码如下:

    [Table("Departments")] public class Department { [Key] public int DeptID { get; set; } public string Name { get; set; } public virtual ICollection<Employee> Employees { get; set; } } [Table("Employees")] public class Employee { [Key] public int EmpID { get; set; } public string Name { get; set; } public int DeptID { get; set; } public virtual Department Department { get; set; } } using (var db = new KLGContext()) { db.Configuration.LazyLoadingEnabled = false; db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ProxyCreatiOnEnabled= false; db.Configuration.ValidateOnSaveEnabled= false; var d1 = db.Department.ToArray(); var e1 = d1.First().Employees;//关闭了懒加载模式, e1 为何还是 null ? var d2 = db.Department.Include("Employees").ToArray(); var e2 = d2.First().Employees;//显示调用上面的 Include 后, e2 有值,必须要显示调用吗? } 
    5 条回复    2016-09-09 09:50:42 +08:00
    rexxiang
        1
    rexxiang  
       2016-09-08 19:16:45 +08:00
    必须显式的 include ,不然,有可能 load 一个对象都有可能把整个 db 都 load 出来么。。。
    Dic4000
        2
    Dic4000  
    OP
       2016-09-08 20:30:44 +08:00
    @rexxiang 那为何 EF 会有这个属性,我把它设置为 false ,就没有什么效果吗?反正都是延迟(懒)加载模式,不提供这个属性也可以啊。
    levon
        3
    levon  
       2016-09-08 20:36:56 +08:00
    去掉属性的 virtual 修饰符试试
    Dic4000
        4
    Dic4000  
    OP
       2016-09-09 07:49:59 +08:00
    @levon 去掉了也不行
    rexxiang
        5
    rexxiang  
       2016-09-09 09:50:42 +08:00   1
    @Dic4000 需不需要延迟加载,是取决于你的需求。错误的使用延迟加载,可能会导致严重的性能问题(N+1 问题)。

    LazyLoadingEnabled 为 true 时:标记为 virtual 的子实体 /集合会在第一次使用的时候从 DB 加载出来。
    LazyLoadingEnabled 为 false 时,意思就是说,你 EF 不要给我延迟加载了,具体加载哪些子对象 /集合,我只会用 Include 的方式告诉你,没有指定的,你都别管。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5456 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 08:09 PVG 16:09 LAX 01:09 JFK 04:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86