实体框架4.1代码优先对延迟加载和虚拟化感到困惑 [英] Entity Framework 4.1 Code First Confused about lazy loading and Virtual

查看:80
本文介绍了实体框架4.1代码优先对延迟加载和虚拟化感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class EntityDataProvider : DbContext, IDataProviderEntity
{
    public EntityDataProvider(string connectionStringName, bool enableLazyLoading = true)
            : base(connectionStringName)
        {
            Configuration.LazyLoadingEnabled = enableLazyLoading;
        }
}


POCO对象帐户:


POCO object Account:

public class Account
{
    ...
        private ICollection<CodeSegment> _codeSegments;
         public virtual ICollection<CodeSegment> CodeSegments
        {
            get
            {
                return _codeSegments ?? (_codeSegments = new Collection<CodeSegment>());
            }
            protected set
            {
                _codeSegments = value;
            }
        }
}


通过上面的代码,我得到了这些结果,这正是我所期望的
CodeSegmets.Count = 4

当我更改以下内容时:


With the code above I get these results, which is what I expect
CodeSegmets.Count = 4

When I change the following:

Configuration.LazyLoadingEnabled = true;
public ICollection<CodeSegment> CodeSegments (removed virtual)



结果:
CodeSegments.Count = 0

变更:



Results:
CodeSegments.Count = 0

Change:

Configuration.LazyLoadingEnabled = false;
public virtual ICollection<CodeSegment> CodeSegments (added virtual)


结果:
CodeSegments.Count = 0


Results:
CodeSegments.Count = 0

Configuration.LazyLoadingEnabled = false;
public ICollection<CodeSegment> CodeSegments (removed virtual)



结果:
CodeSegments.Count = 0



Results:
CodeSegments.Count = 0

Can anyone shed some light on this?

推荐答案

没有虚拟功能,您肯定没有延迟加载.
如果没有配置禁用该功能,第二个示例将延迟加载.因此,这里不需要混淆.
Without virtual you definitely don''t have lazy loading.
The second example would lazy load, if you did not have the configuration disabled that feature. So there is no need for confusion here.


这篇关于实体框架4.1代码优先对延迟加载和虚拟化感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆