简体   繁体   中英

entity framework lazy loading problen

I am facing a problem. After running a select statement I make lazy loading false. In that case, reference objects are null. But when debug my code everything works fine.referencr are loaded correctly. if I put waiting time before bl.LazyLoading(false) then it also works. 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.

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:

  1. Use include method;
  2. Virtual collection into entity class;
  3. Select() method;
  4. Entry() and Load() methods;

Approaches 3 and 4 will still load possible related entities;

More detailed you could read here: https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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