简体   繁体   English

实体框架延迟加载问题

[英]entity framework lazy loading problen

I am facing a problem. 我面临一个问题。 After running a select statement I make lazy loading false. 运行select语句后,我将延迟加载设置为false。 In that case, reference objects are null. 在这种情况下,引用对象为null。 But when debug my code everything works fine.referencr are loaded correctly. 但是当调试我的代码时,一切都正常。referencr已正确加载。 if I put waiting time before bl.LazyLoading(false) then it also works. 如果我在bl.LazyLoading(false)之前放置等待时间,那么它也可以工作。 Is there any way to detect reference are loaded? 有什么方法可以检测引用是否已加载? I have to wait until reference loaded. 我必须等到引用加载完毕。 Is their any better Idea?I am not interested to use include() or reference() method. 他们有更好的主意吗?我对使用include()或reference()方法不感兴趣。

using (var bl = new BusinessLayer<T>())
{
    bl.LazyLoading(true);
    var list = bl.GetAll();
    //wait
    bl.LazyLoading(false);
    if (list.Any())
    {
        System.Web.HttpContext.Current.Cache.Insert(key, list, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
    }
    else
    {
        _logger.WriteInfo(String.Format("{0} list is empty.", key));
        throw new Exception(String.Format("{0} list is empty.", typeof(T).Name));
    }
}

Entity Framework has 4 different method for implementing lazy loading mechanism: 实体框架有4种不同的方法来实现延迟加载机制:

  1. Use include method; 使用包含方法;
  2. Virtual collection into entity class; 虚拟集合到实体类中;
  3. Select() method; Select()方法;
  4. Entry() and Load() methods; Entry()和Load()方法;

Approaches 3 and 4 will still load possible related entities; 方法3和4仍将加载可能的相关实体;

More detailed you could read here: https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx 更详细的信息可以在这里阅读: https : //msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM