18

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; } 
    }
}
Amirhossein Mehrvarzi
  • 11,037
  • 6
  • 38
  • 65
Barry
  • 1,064
  • 1
  • 7
  • 21

3 Answers3

39

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).

Community
  • 1
  • 1
Gert Arnold
  • 93,904
  • 24
  • 179
  • 256
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

Abhishek Punj
  • 279
  • 2
  • 6
  • Thanks for the reply, I know about .include() but would prefer to eager load it by default using the non-virtual property method. – Barry Sep 20 '13 at 13:10
  • 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 http://stackoverflow.com/questions/14512285/entity-framework-is-there-a-way-to-automatically-eager-load-child-entities-wit – Abhishek Punj Sep 20 '13 at 13:33
  • see: http://msdn.microsoft.com/en-us/data/jj574232.aspx "Turning off lazy loading for specific navigation properties" – Barry Sep 20 '13 at 13:49
  • 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. – Barry Sep 23 '13 at 07:56
-2

Just one thing mention here.

If I turn off EF's LazyLoading with

this.DbContext.Configuration.LazyLoadingEnabled = false;

then, "include" method will not loading child entities for Eagerly loading.

so, if i want to use "include" in the query, I need to turn on EF's LazyLoading property at same time.

ChinaHelloWorld
  • 775
  • 1
  • 8
  • 6
  • 1
    Ehh, this is simply not true. Can you give an example where you encountered this? Something must have been going on. – Gert Arnold May 01 '14 at 22:20
  • 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). – Bennit Feb 05 '15 at 09:05