Lazy Loading vs Eager Loading

前端 未结 8 1337
予麋鹿
予麋鹿 2020-11-27 10:36

Lazy loading in Entity Framework is the default phenomenon that happens for loading and accessing the related entities. However, eager loading is referred to the practice of

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 10:50

    Consider the below situation

    public class Person{
        public String Name{get; set;}
        public String Email {get; set;}
        public virtual Employer employer {get; set;}
    }
    
    public List GetPerson(){
        using(EF.DbEntities db = new EF.DbEntities()){
           return db.Person.ToList();
        }
    }
    

    Now after this method is called, you cannot lazy load the Employer entity anymore. Why? because the db object is disposed. So you have to do Person.Include(x=> x.employer) to force that to be loaded.

提交回复
热议问题