Entity Framework Eager Load Not Returning Data, Lazy Load Does

16,859

Solution 1

This is a common confusion. The opposite of lazy loading is: no loading unless you explicitly do the loading yourself (e.g. by eager loading using Include).

So if you turn off lazy loading in any way — removing the virtual modifier is one of them — the behaviour does not turn into eager loading but no loading.

Think of it, suppose EF would eagerly load everything that is not marked for lazy loading. You run the risk of loading half the database by doing one simple query!

There is no way to make a navigation property eager loading by default (if you'd still want that after reading the above).

Solution 2

You will need to use the include method to force load of the ICollections within your entities with eager loading. The follwing link might help you: http://msdn.microsoft.com/en-us/data/jj574232.aspx

Share:
16,859

Related videos on Youtube

Barry
Author by

Barry

Updated on November 05, 2022

Comments

  • Barry
    Barry over 1 year

    I'm using code first EF5 and I have an object which has a collection defined as virtual (lazy loaded). This returns data when called. However I want it to be eager loaded. I've removed virtual from the property signature but now it always returns null data. EF doesn't even run the query, can anyone help?

    Edit: I know about .include() I'd just prefer to use the non-virtual property method of doing it.

    Objects

    User ([Key] Id is on Resource object which is the Parent of person class):

    namespace Entities
    {
        [Table("Users")]
        public class User : Person
        {
    
        [Required]
        public ICollection<Role> Roles { get; set; } 
    
        }
    }
    

    Role:

    namespace Entities
    {
        public class Role
        {
            [Key]
            public string Id { get; set; }
    
            public virtual ICollection<User> Users { get; set; } 
        }
    }
    
  • Barry
    Barry over 10 years
    Thanks for the reply, I know about .include() but would prefer to eager load it by default using the non-virtual property method.
  • Abhishek Punj
    Abhishek Punj over 10 years
    As far as I know its not available by default.. you will need to include these somewhere or the other. For ways to do it check out this post stackoverflow.com/questions/14512285/…
  • Barry
    Barry over 10 years
    see: msdn.microsoft.com/en-us/data/jj574232.aspx "Turning off lazy loading for specific navigation properties"
  • Barry
    Barry over 10 years
    Apologies, thanks to Gurts comment I understand it now (was the way it was written on msdn that spun me). The extension method is a pretty nice way to do it, cheers for that.
  • Gert Arnold
    Gert Arnold almost 10 years
    Ehh, this is simply not true. Can you give an example where you encountered this? Something must have been going on.
  • Bennit
    Bennit about 9 years
    I know this post is a bit dated, but I'm experiencing the same issue (EF6, model first, having disabled LazyLoading via annotation in the edmx).
  • rohitwtbs
    rohitwtbs over 7 years
    The tip about removing the virtual switch off the lazy load save my day ;). Thanks!!!