Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core - Lazy Loading

Bowing to my Visual Studios request, I started my latest project using Entity Framework Core (1.0.1)

So writing my database models as I always have using the 'virtual' specifier to enable lazy loading for a List. Though when loading the parent table it appears that the child list never loads.

Parent Model

public class Events {     [Key]      public int EventID { get; set; }     public string EventName { get; set; }     public virtual List<EventInclusions> EventInclusions { get; set; } } 

Child Model

public class EventInclusions {     [Key]     public int EventIncSubID { get; set; }     public string InclusionName { get; set; }     public string InclusionDesc { get; set; }     public Boolean InclusionActive { get; set; }  } 

Adding new records to these tables seems to work as I am used to where I can nest the EventInclusions records as a List inside the Events record.

Though when I query this table

_context.Events.Where(e => e.EventName == "Test") 

The Issue

EventInclusions will return a null value regardless of the data behind the scenes.

After reading a bit I am getting the feeling this is a change between EF6 which I normally use and EF Core

I could use some help in either making a blanket Lazy Loading on statement or figuring out the new format for specifying Lazy Loading.

Caz

like image 704
Caz1224 Avatar asked Oct 19 '16 04:10

Caz1224


People also ask

Is lazy loading supported in EF core?

EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. For example, in the following entities, the Post. Blog and Blog. Posts navigation properties will be lazy-loaded.

How do I set lazy loading in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

What is eager loading and lazy loading in Entity Framework Core?

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 force-loading all these relations.


2 Answers

Lazy loading is now available on EF Core 2.1 and here is link to the relevant docs:

https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading

like image 97
Elijah Lofgren Avatar answered Oct 14 '22 10:10

Elijah Lofgren


So it appears that EF Core does not currently support lazy loading. Its coming but may be a while off.

For now if anyone else comes across this problem and is struggling. Below is a demo of using Eager loading which is what for now you have to use.

Say before you had a person object and that object contained a List of Hats in another table.

Rather than writing

var person = _context.Person.Where(p=> p.id == id).ToList();  person.Hats.Where(h=> h.id == hat).ToList(); 

You need to write

var person = _context.Person.Include(p=> p.Hats).Where(p=> p.id == id).ToList(); 

And then person.Hats.Where(h=> h.id == hat).ToList(); will work

If you have multiple Lists - Chain the Includes

var person = _context.Person.Include(p=> p.Hats).Include(p=> p.Tickets)                             .Include(p=> p.Smiles).Where(p=> p.id == id).ToList(); 

I kinda get why this method is safer, that your not loading huge data sets that could slow things down. But I hope they get Lazy loading back soon!!!

Caz

like image 43
Caz1224 Avatar answered Oct 14 '22 08:10

Caz1224