EF Code First forced eager loading

code-first ef-code-first entity-framework

Question

I'm using Code First and EF 5. I have a class whose attributes I want to constantly eagerly load. Zzz-5-Zzz was deleted, but it's still not eagerly loading:

public class Person
{
   public ICollection<Email> Emails { get; set; } 
   public Profile Profile {get;set;}
}

So it won't automatically eager load if lazy loading is off, correct? Using Include() is not an option, thus how can I archive it instead?

Thanks!

1
8
8/22/2012 12:32:34 AM

Accepted Answer

No, disabling lazy loading by taking away thevirtual keyword won't turn on eager loading automatically. You must.Include the connectedEntity or Collection like this:

var personWithProfile = ctx.People.Include(x => x.Profile).First();
var personWithProfileAndEmails = ctx.People.
                                           .Include(x => x.Profile)
                                           .Include(x => x.Emails)
                                           .First();

This post from the ADO.NET team blog is fantastic to read: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

18
8/22/2012 12:41:07 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow